FLEXArgumentInputFontsPickerView.m 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. [(UIPickerView *)self.inputTextView.inputView selectRow:[self.availableFonts indexOfObject:inputValue] inComponent:0 animated:NO];
  40. }
  41. - (id)inputValue {
  42. return self.inputTextView.text.length > 0 ? [self.inputTextView.text copy] : nil;
  43. }
  44. #pragma mark - private
  45. - (UIPickerView*)createFontsPicker {
  46. UIPickerView *fontsPicker = [UIPickerView new];
  47. fontsPicker.dataSource = self;
  48. fontsPicker.delegate = self;
  49. fontsPicker.showsSelectionIndicator = YES;
  50. return fontsPicker;
  51. }
  52. - (void)createAvailableFonts {
  53. NSMutableArray<NSString *> *unsortedFontsArray = [NSMutableArray new];
  54. for (NSString *eachFontFamily in UIFont.familyNames) {
  55. for (NSString *eachFontName in [UIFont fontNamesForFamilyName:eachFontFamily]) {
  56. [unsortedFontsArray addObject:eachFontName];
  57. }
  58. }
  59. self.availableFonts = [NSMutableArray arrayWithArray:[unsortedFontsArray sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)]];
  60. }
  61. #pragma mark - UIPickerViewDataSource
  62. - (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
  63. return 1;
  64. }
  65. - (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
  66. return self.availableFonts.count;
  67. }
  68. #pragma mark - UIPickerViewDelegate
  69. - (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view {
  70. UILabel *fontLabel;
  71. if (!view) {
  72. fontLabel = [UILabel new];
  73. fontLabel.backgroundColor = UIColor.clearColor;
  74. fontLabel.textAlignment = NSTextAlignmentCenter;
  75. } else {
  76. fontLabel = (UILabel*)view;
  77. }
  78. UIFont *font = [UIFont fontWithName:self.availableFonts[row] size:15.0];
  79. NSDictionary<NSString *, id> *attributesDictionary = [NSDictionary<NSString *, id> dictionaryWithObject:font forKey:NSFontAttributeName];
  80. NSAttributedString *attributesString = [[NSAttributedString alloc] initWithString:self.availableFonts[row] attributes:attributesDictionary];
  81. fontLabel.attributedText = attributesString;
  82. [fontLabel sizeToFit];
  83. return fontLabel;
  84. }
  85. - (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
  86. self.inputTextView.text = self.availableFonts[row];
  87. }
  88. @end