FLEXArgumentInputFontsPickerView.m 3.2 KB

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