FLEXArgumentInputFontsPickerView.m 3.2 KB

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