FLEXArgumentInputFontsPickerView.m 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. //
  2. // FLEXArgumentInputFontsPickerView.m
  3. // UICatalog
  4. //
  5. // Created by 啟倫 陳 on 2014/7/27.
  6. // Copyright (c) 2014年 f. All rights reserved.
  7. //
  8. #import "FLEXArgumentInputFontsPickerView.h"
  9. #import "FLEXRuntimeUtility.h"
  10. @interface FLEXArgumentInputFontsPickerView ()
  11. @property (nonatomic, strong) NSArray *availableFonts;
  12. @end
  13. @implementation FLEXArgumentInputFontsPickerView
  14. - (instancetype)initWithArgumentTypeEncoding:(const char *)typeEncoding
  15. {
  16. self = [super initWithArgumentTypeEncoding:typeEncoding];
  17. if (self) {
  18. [self createAvailableFonts];
  19. self.targetSize = FLEXArgumentInputViewSizeSmall;
  20. self.inputTextView.inputView = ({
  21. UIPickerView *fontsPicker = [UIPickerView new];
  22. fontsPicker.dataSource = self;
  23. fontsPicker.delegate = self;
  24. fontsPicker.showsSelectionIndicator = YES;
  25. fontsPicker;
  26. });
  27. }
  28. return self;
  29. }
  30. - (void)setInputValue:(id)inputValue
  31. {
  32. self.inputTextView.text = inputValue;
  33. [(UIPickerView*)self.inputTextView.inputView selectRow:[self.availableFonts indexOfObject:inputValue]
  34. inComponent:0
  35. animated:NO];
  36. }
  37. - (id)inputValue
  38. {
  39. return [self.inputTextView.text length] > 0 ? [self.inputTextView.text copy] : nil;
  40. }
  41. #pragma mark - private
  42. - (void)textFieldDone {
  43. }
  44. - (void)createAvailableFonts
  45. {
  46. NSMutableArray *unsortedFontsArray = [NSMutableArray array];
  47. for (NSString *eachFontFamily in [UIFont familyNames]) {
  48. for (NSString *eachFontName in [UIFont fontNamesForFamilyName:eachFontFamily]) {
  49. [unsortedFontsArray addObject:eachFontName];
  50. }
  51. }
  52. self.availableFonts = [unsortedFontsArray sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
  53. }
  54. #pragma mark - UIPickerViewDataSource
  55. - (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
  56. {
  57. return 1;
  58. }
  59. - (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
  60. {
  61. return [self.availableFonts count];
  62. }
  63. #pragma mark - UIPickerViewDelegate
  64. - (NSAttributedString *)pickerView:(UIPickerView *)pickerView attributedTitleForRow:(NSInteger)row forComponent:(NSInteger)component
  65. {
  66. UIFont *font = [UIFont fontWithName:self.availableFonts[row] size:14.0];
  67. NSDictionary *attributesDictionary = [NSDictionary dictionaryWithObject:font
  68. forKey:NSFontAttributeName];
  69. NSAttributedString *attrString = [[NSAttributedString alloc] initWithString:self.availableFonts[row] attributes:attributesDictionary];
  70. return attrString;
  71. }
  72. - (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
  73. {
  74. self.inputTextView.text = self.availableFonts[row];
  75. }
  76. #pragma mark -
  77. + (BOOL)supportsObjCType:(const char *)type withCurrentValue:(id)value
  78. {
  79. BOOL supported = type && strcmp(type, FLEXEncodeClass(FLEXArgumentInputFontsPickerView)) == 0;
  80. supported = supported || (value && [value isKindOfClass:[FLEXArgumentInputFontsPickerView class]]);
  81. return supported;
  82. }
  83. @end