FLEXArgumentInputFontsPickerView.m 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. //
  2. // FLEXArgumentInputFontsPickerView.m
  3. // FLEX
  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. #import "FLEXFontListTableViewController.h"
  11. #import "NSObject+FLEX_Reflection.h"
  12. @interface FLEXArgumentInputFontsPickerView ()
  13. @property (nonatomic) NSMutableArray<NSString *> *availableFonts;
  14. @end
  15. @implementation FLEXArgumentInputFontsPickerView
  16. - (instancetype)initWithArgumentTypeEncoding:(const char *)typeEncoding {
  17. self = [super initWithArgumentTypeEncoding:typeEncoding];
  18. if (self) {
  19. self.targetSize = FLEXArgumentInputViewSizeSmall;
  20. [self createAvailableFonts];
  21. #if TARGET_OS_TV
  22. FLEXFontListTableViewController *fontListController = [FLEXFontListTableViewController new];
  23. fontListController.itemSelectedBlock = ^(NSString *fontName) {
  24. [self.inputTextView setText:fontName];
  25. [[self topViewController] dismissViewControllerAnimated:true completion:nil];
  26. };
  27. self.inputTextView.inputViewController = fontListController;
  28. #else
  29. self.inputTextView.inputView = [self createFontsPicker];
  30. #endif
  31. }
  32. return self;
  33. }
  34. - (void)setInputValue:(id)inputValue {
  35. self.inputTextView.text = inputValue;
  36. if ([self.availableFonts indexOfObject:inputValue] == NSNotFound) {
  37. [self.availableFonts insertObject:inputValue atIndex:0];
  38. }
  39. #if !TARGET_OS_TV
  40. [(UIPickerView *)self.inputTextView.inputView selectRow:[self.availableFonts indexOfObject:inputValue] inComponent:0 animated:NO];
  41. #endif
  42. }
  43. - (id)inputValue {
  44. return self.inputTextView.text.length > 0 ? [self.inputTextView.text copy] : nil;
  45. }
  46. #pragma mark - private
  47. - (void)createAvailableFonts {
  48. NSMutableArray<NSString *> *unsortedFontsArray = [NSMutableArray new];
  49. for (NSString *eachFontFamily in UIFont.familyNames) {
  50. for (NSString *eachFontName in [UIFont fontNamesForFamilyName:eachFontFamily]) {
  51. [unsortedFontsArray addObject:eachFontName];
  52. }
  53. }
  54. self.availableFonts = [NSMutableArray arrayWithArray:[unsortedFontsArray sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)]];
  55. }
  56. #if !TARGET_OS_TV
  57. - (UIPickerView*)createFontsPicker {
  58. UIPickerView *fontsPicker = [UIPickerView new];
  59. fontsPicker.dataSource = self;
  60. fontsPicker.delegate = self;
  61. fontsPicker.showsSelectionIndicator = YES;
  62. return fontsPicker;
  63. }
  64. #pragma mark - UIPickerViewDataSource
  65. - (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
  66. return 1;
  67. }
  68. - (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
  69. return self.availableFonts.count;
  70. }
  71. #pragma mark - UIPickerViewDelegate
  72. - (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view {
  73. UILabel *fontLabel;
  74. if (!view) {
  75. fontLabel = [UILabel new];
  76. fontLabel.backgroundColor = UIColor.clearColor;
  77. fontLabel.textAlignment = NSTextAlignmentCenter;
  78. } else {
  79. fontLabel = (UILabel*)view;
  80. }
  81. UIFont *font = [UIFont fontWithName:self.availableFonts[row] size:15.0];
  82. NSDictionary<NSString *, id> *attributesDictionary = [NSDictionary<NSString *, id> dictionaryWithObject:font forKey:NSFontAttributeName];
  83. NSAttributedString *attributesString = [[NSAttributedString alloc] initWithString:self.availableFonts[row] attributes:attributesDictionary];
  84. fontLabel.attributedText = attributesString;
  85. [fontLabel sizeToFit];
  86. return fontLabel;
  87. }
  88. - (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
  89. self.inputTextView.text = self.availableFonts[row];
  90. }
  91. #endif
  92. @end