FLEXArgumentInputFontsPickerView.m 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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] 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 *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 *attributesDictionary = [NSDictionary 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