You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

105 lines
3.2 KiB

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