Kaynağa Gözat

Merge pull request #7 from DaidoujiChen/develop

Add font picker input view
Ryan Olson 12 yıl önce
ebeveyn
işleme
2862c3ee97

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

@@ -9,6 +9,7 @@
 #import "FLEXArgumentInputFontView.h"
 #import "FLEXArgumentInputViewFactory.h"
 #import "FLEXRuntimeUtility.h"
+#import "FLEXArgumentInputFontsPickerView.h"
 
 @interface FLEXArgumentInputFontView ()
 
@@ -23,7 +24,7 @@
 {
     self = [super initWithArgumentTypeEncoding:typeEncoding];
     if (self) {
-        self.fontNameInput = [FLEXArgumentInputViewFactory argumentInputViewForTypeEncoding:FLEXEncodeClass(NSString)];
+        self.fontNameInput = [[FLEXArgumentInputFontsPickerView alloc] initWithArgumentTypeEncoding:FLEXEncodeClass(NSString)];
         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

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

@@ -0,0 +1,105 @@
+//
+//  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) NSMutableArray *availableFonts;
+
+@end
+
+
+@implementation FLEXArgumentInputFontsPickerView
+
+- (instancetype)initWithArgumentTypeEncoding:(const char *)typeEncoding
+{
+    self = [super initWithArgumentTypeEncoding:typeEncoding];
+    if (self) {
+        self.targetSize = FLEXArgumentInputViewSizeSmall;
+        [self createAvailableFonts];
+        self.inputTextView.inputView = [self createFontsPicker];
+    }
+    return self;
+}
+
+- (void)setInputValue:(id)inputValue
+{
+    self.inputTextView.text = inputValue;
+    if ([self.availableFonts indexOfObject:inputValue] == NSNotFound) {
+        [self.availableFonts insertObject:inputValue atIndex:0];
+    }
+    [(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
+
+- (UIPickerView*)createFontsPicker
+{
+    UIPickerView *fontsPicker = [UIPickerView new];
+    fontsPicker.dataSource = self;
+    fontsPicker.delegate = self;
+    fontsPicker.showsSelectionIndicator = YES;
+    return fontsPicker;
+}
+
+- (void)createAvailableFonts
+{
+    NSMutableArray *unsortedFontsArray = [NSMutableArray array];
+    for (NSString *eachFontFamily in [UIFont familyNames]) {
+        for (NSString *eachFontName in [UIFont fontNamesForFamilyName:eachFontFamily]) {
+            [unsortedFontsArray addObject:eachFontName];
+        }
+    }
+    self.availableFonts = [NSMutableArray arrayWithArray:[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
+
+- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
+{
+    UILabel *fontLabel;
+    if (!view) {
+        fontLabel = [UILabel new];
+        fontLabel.backgroundColor = [UIColor clearColor];
+        fontLabel.textAlignment = NSTextAlignmentCenter;
+    } else {
+        fontLabel = (UILabel*)view;
+    }
+    UIFont *font = [UIFont fontWithName:self.availableFonts[row] size:15.0];
+    NSDictionary *attributesDictionary = [NSDictionary dictionaryWithObject:font forKey:NSFontAttributeName];
+    NSAttributedString *attributesString = [[NSAttributedString alloc] initWithString:self.availableFonts[row] attributes:attributesDictionary];
+    fontLabel.attributedText = attributesString;
+    [fontLabel sizeToFit];
+    return fontLabel;
+}
+
+- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
+{
+    self.inputTextView.text = self.availableFonts[row];
+}
+
+@end

+ 18 - 0
Classes/Editing/Argument Input Views/FLEXArgumentInputTextView.m

@@ -30,11 +30,29 @@
         self.inputTextView.autocapitalizationType = UITextAutocapitalizationTypeNone;
         self.inputTextView.autocorrectionType = UITextAutocorrectionTypeNo;
         self.inputTextView.delegate = self;
+        self.inputTextView.inputAccessoryView = [self createToolBar];
         [self addSubview:self.inputTextView];
     }
     return self;
 }
 
+#pragma mark - private
+
+- (UIToolbar*)createToolBar
+{
+    UIToolbar *toolBar = [UIToolbar new];
+    [toolBar sizeToFit];
+    UIBarButtonItem *spaceItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
+    UIBarButtonItem *doneItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(textViewDone)];
+    toolBar.items = @[spaceItem, doneItem];
+    return toolBar;
+}
+
+- (void)textViewDone
+{
+    [self.inputTextView resignFirstResponder];
+}
+
 
 #pragma mark - Text View Changes
 

+ 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 */,