Просмотр исходного кода

add FLEXArgumentInputFontsPickerView, easily select fonts

DaidoujiChen лет назад: 12
Родитель
Сommit
842bc94525

+ 1 - 1
Classes/Editing/Argument Input Views/FLEXArgumentInputFontView.m

@@ -23,7 +23,7 @@
 {
     self = [super initWithArgumentTypeEncoding:typeEncoding];
     if (self) {
-        self.fontNameInput = [FLEXArgumentInputViewFactory argumentInputViewForTypeEncoding:FLEXEncodeClass(NSString)];
+        self.fontNameInput = [FLEXArgumentInputViewFactory argumentInputViewForTypeEncoding:FLEXEncodeClass(FLEXArgumentInputFontsPickerView)];
         self.fontNameInput.backgroundColor = self.backgroundColor;
         self.fontNameInput.targetSize = FLEXArgumentInputViewSizeSmall;
         self.fontNameInput.title = @"Font Name:";

+ 12 - 0
Classes/Editing/Argument Input Views/FLEXArgumentInputFontsPickerView.h

@@ -0,0 +1,12 @@
+//
+//  FLEXArgumentInputFontsPickerView.h
+//  UICatalog
+//
+//  Created by 啟倫 陳 on 2014/7/27.
+//  Copyright (c) 2014年 f. All rights reserved.
+//
+
+#import "FLEXArgumentInputTextView.h"
+
+@interface FLEXArgumentInputFontsPickerView : FLEXArgumentInputTextView <UIPickerViewDataSource, UIPickerViewDelegate>
+@end

+ 115 - 0
Classes/Editing/Argument Input Views/FLEXArgumentInputFontsPickerView.m

@@ -0,0 +1,115 @@
+//
+//  FLEXArgumentInputFontsPickerView.m
+//  UICatalog
+//
+//  Created by 啟倫 陳 on 2014/7/27.
+//  Copyright (c) 2014年 f. All rights reserved.
+//
+
+#import "FLEXArgumentInputFontsPickerView.h"
+#import "FLEXRuntimeUtility.h"
+
+@interface FLEXArgumentInputFontsPickerView ()
+
+@property (nonatomic, strong) NSArray *availableFonts;
+
+@end
+
+
+@implementation FLEXArgumentInputFontsPickerView
+
+- (instancetype)initWithArgumentTypeEncoding:(const char *)typeEncoding
+{
+    self = [super initWithArgumentTypeEncoding:typeEncoding];
+    if (self) {
+        
+        [self createAvailableFonts];
+        
+        self.targetSize = FLEXArgumentInputViewSizeSmall;
+        
+        self.inputTextView.inputView = ({
+            UIPickerView *fontsPicker = [UIPickerView new];
+            fontsPicker.dataSource = self;
+            fontsPicker.delegate = self;
+            fontsPicker.showsSelectionIndicator = YES;
+            fontsPicker;
+        });
+        
+    }
+    return self;
+}
+
+- (void)setInputValue:(id)inputValue
+{
+    self.inputTextView.text = inputValue;
+    [(UIPickerView*)self.inputTextView.inputView selectRow:[self.availableFonts indexOfObject:inputValue]
+                                               inComponent:0
+                                                  animated:NO];
+}
+
+- (id)inputValue
+{
+    return [self.inputTextView.text length] > 0 ? [self.inputTextView.text copy] : nil;
+}
+
+#pragma mark - private
+
+- (void)textFieldDone {
+    
+}
+
+- (void)createAvailableFonts
+{
+    NSMutableArray *unsortedFontsArray = [NSMutableArray array];
+
+    for (NSString *eachFontFamily in [UIFont familyNames]) {
+        
+        for (NSString *eachFontName in [UIFont fontNamesForFamilyName:eachFontFamily]) {
+            [unsortedFontsArray addObject:eachFontName];
+        }
+        
+    }
+    
+    self.availableFonts = [unsortedFontsArray sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
+}
+
+#pragma mark - UIPickerViewDataSource
+
+- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
+{
+    return 1;
+}
+
+- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
+{
+    
+    return [self.availableFonts count];
+    
+}
+
+#pragma mark - UIPickerViewDelegate
+
+- (NSAttributedString *)pickerView:(UIPickerView *)pickerView attributedTitleForRow:(NSInteger)row forComponent:(NSInteger)component
+{
+    UIFont *font = [UIFont fontWithName:self.availableFonts[row] size:14.0];
+    NSDictionary *attributesDictionary = [NSDictionary dictionaryWithObject:font
+                                                                     forKey:NSFontAttributeName];
+    NSAttributedString *attrString = [[NSAttributedString alloc] initWithString:self.availableFonts[row] attributes:attributesDictionary];
+    return attrString;
+}
+
+- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
+{
+    self.inputTextView.text = self.availableFonts[row];
+}
+
+#pragma mark -
+
++ (BOOL)supportsObjCType:(const char *)type withCurrentValue:(id)value
+{
+    BOOL supported = type && strcmp(type, FLEXEncodeClass(FLEXArgumentInputFontsPickerView)) == 0;
+    supported = supported || (value && [value isKindOfClass:[FLEXArgumentInputFontsPickerView class]]);
+    return supported;
+}
+
+@end

+ 3 - 0
Classes/Editing/Argument Input Views/FLEXArgumentInputViewFactory.m

@@ -16,6 +16,7 @@
 #import "FLEXArgumentInputStringView.h"
 #import "FLEXArgumentInputFontView.h"
 #import "FLEXArgumentInputColorView.h"
+#import "FLEXArgumentInputFontsPickerView.h"
 
 @implementation FLEXArgumentInputViewFactory
 
@@ -51,6 +52,8 @@
         argumentInputViewSubclass = [FLEXArgumentInputNumberView class];
     } else if ([FLEXArgumentInputJSONObjectView supportsObjCType:typeEncoding withCurrentValue:currentValue]) {
         argumentInputViewSubclass = [FLEXArgumentInputJSONObjectView class];
+    } else if ([FLEXArgumentInputFontsPickerView supportsObjCType:typeEncoding withCurrentValue:currentValue]) {
+        argumentInputViewSubclass = [FLEXArgumentInputFontsPickerView class];
     }
     
     return argumentInputViewSubclass;

+ 6 - 0
Example/UICatalog.xcodeproj/project.pbxproj

@@ -7,6 +7,7 @@
 	objects = {
 
 /* Begin PBXBuildFile section */
+		01985ABC1984DE9500A65332 /* FLEXArgumentInputFontsPickerView.m in Sources */ = {isa = PBXBuildFile; fileRef = 01985ABB1984DE9500A65332 /* FLEXArgumentInputFontsPickerView.m */; };
 		3EC6487318FF8A5000024205 /* ReadMe.txt in Resources */ = {isa = PBXBuildFile; fileRef = 3EC6487218FF8A5000024205 /* ReadMe.txt */; };
 		5356823E18F3656900BAAD62 /* Foundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 5356823D18F3656900BAAD62 /* Foundation.framework */; };
 		5356824018F3656900BAAD62 /* CoreGraphics.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 5356823F18F3656900BAAD62 /* CoreGraphics.framework */; };
@@ -92,6 +93,8 @@
 /* End PBXBuildFile section */
 
 /* Begin PBXFileReference section */
+		01985ABA1984DE9500A65332 /* FLEXArgumentInputFontsPickerView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = FLEXArgumentInputFontsPickerView.h; sourceTree = "<group>"; };
+		01985ABB1984DE9500A65332 /* FLEXArgumentInputFontsPickerView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = FLEXArgumentInputFontsPickerView.m; sourceTree = "<group>"; };
 		3EC6487218FF8A5000024205 /* ReadMe.txt */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = ReadMe.txt; sourceTree = SOURCE_ROOT; };
 		5356823A18F3656900BAAD62 /* UICatalog.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = UICatalog.app; sourceTree = BUILT_PRODUCTS_DIR; };
 		5356823D18F3656900BAAD62 /* Foundation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Foundation.framework; path = System/Library/Frameworks/Foundation.framework; sourceTree = SDKROOT; };
@@ -495,6 +498,8 @@
 				944F744F197B458C009AB039 /* FLEXArgumentInputNumberView.m */,
 				944F7450197B458C009AB039 /* FLEXArgumentInputStringView.h */,
 				944F7451197B458C009AB039 /* FLEXArgumentInputStringView.m */,
+				01985ABA1984DE9500A65332 /* FLEXArgumentInputFontsPickerView.h */,
+				01985ABB1984DE9500A65332 /* FLEXArgumentInputFontsPickerView.m */,
 				944F7452197B458C009AB039 /* FLEXArgumentInputStructView.h */,
 				944F7453197B458C009AB039 /* FLEXArgumentInputStructView.m */,
 				944F7454197B458C009AB039 /* FLEXArgumentInputSwitchView.h */,
@@ -703,6 +708,7 @@
 				535682B818F3670300BAAD62 /* AAPLSplitViewControllerDelegate.m in Sources */,
 				535682AC18F3670300BAAD62 /* AAPLCustomSearchBarViewController.m in Sources */,
 				535682A718F3670300BAAD62 /* AAPLActionSheetViewController.m in Sources */,
+				01985ABC1984DE9500A65332 /* FLEXArgumentInputFontsPickerView.m in Sources */,
 				944F74AA197B458C009AB039 /* FLEXExplorerViewController.m in Sources */,
 				944F7492197B458C009AB039 /* FLEXViewControllerExplorerViewController.m in Sources */,
 				5356824A18F3656900BAAD62 /* main.m in Sources */,