FLEXArgumentInputFontView.m 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. //
  2. // FLEXArgumentInputFontView.m
  3. // Flipboard
  4. //
  5. // Created by Ryan Olson on 6/28/14.
  6. // Copyright (c) 2020 FLEX Team. All rights reserved.
  7. //
  8. #import "FLEXArgumentInputFontView.h"
  9. #import "FLEXArgumentInputViewFactory.h"
  10. #import "FLEXRuntimeUtility.h"
  11. #import "FLEXArgumentInputFontsPickerView.h"
  12. @interface FLEXArgumentInputFontView ()
  13. @property (nonatomic) FLEXArgumentInputView *fontNameInput;
  14. @property (nonatomic) FLEXArgumentInputView *pointSizeInput;
  15. @end
  16. @implementation FLEXArgumentInputFontView
  17. - (instancetype)initWithArgumentTypeEncoding:(const char *)typeEncoding {
  18. self = [super initWithArgumentTypeEncoding:typeEncoding];
  19. if (self) {
  20. self.fontNameInput = [[FLEXArgumentInputFontsPickerView alloc] initWithArgumentTypeEncoding:FLEXEncodeClass(NSString)];
  21. self.fontNameInput.targetSize = FLEXArgumentInputViewSizeSmall;
  22. self.fontNameInput.title = @"Font Name:";
  23. [self addSubview:self.fontNameInput];
  24. self.pointSizeInput = [FLEXArgumentInputViewFactory argumentInputViewForTypeEncoding:@encode(CGFloat)];
  25. self.pointSizeInput.targetSize = FLEXArgumentInputViewSizeSmall;
  26. self.pointSizeInput.title = @"Point Size:";
  27. [self addSubview:self.pointSizeInput];
  28. }
  29. return self;
  30. }
  31. - (void)setBackgroundColor:(UIColor *)backgroundColor {
  32. [super setBackgroundColor:backgroundColor];
  33. self.fontNameInput.backgroundColor = backgroundColor;
  34. self.pointSizeInput.backgroundColor = backgroundColor;
  35. }
  36. - (void)setInputValue:(id)inputValue {
  37. if ([inputValue isKindOfClass:[UIFont class]]) {
  38. UIFont *font = (UIFont *)inputValue;
  39. self.fontNameInput.inputValue = font.fontName;
  40. self.pointSizeInput.inputValue = @(font.pointSize);
  41. }
  42. }
  43. - (id)inputValue {
  44. CGFloat pointSize = 0;
  45. if ([self.pointSizeInput.inputValue isKindOfClass:[NSValue class]]) {
  46. NSValue *pointSizeValue = (NSValue *)self.pointSizeInput.inputValue;
  47. if (strcmp([pointSizeValue objCType], @encode(CGFloat)) == 0) {
  48. [pointSizeValue getValue:&pointSize];
  49. }
  50. }
  51. return [UIFont fontWithName:self.fontNameInput.inputValue size:pointSize];
  52. }
  53. - (BOOL)inputViewIsFirstResponder {
  54. return [self.fontNameInput inputViewIsFirstResponder] || [self.pointSizeInput inputViewIsFirstResponder];
  55. }
  56. #pragma mark - Layout and Sizing
  57. - (void)layoutSubviews {
  58. [super layoutSubviews];
  59. CGFloat runningOriginY = self.topInputFieldVerticalLayoutGuide;
  60. CGSize fontNameFitSize = [self.fontNameInput sizeThatFits:self.bounds.size];
  61. self.fontNameInput.frame = CGRectMake(0, runningOriginY, fontNameFitSize.width, fontNameFitSize.height);
  62. runningOriginY = CGRectGetMaxY(self.fontNameInput.frame) + [[self class] verticalPaddingBetweenFields];
  63. CGSize pointSizeFitSize = [self.pointSizeInput sizeThatFits:self.bounds.size];
  64. self.pointSizeInput.frame = CGRectMake(0, runningOriginY, pointSizeFitSize.width, pointSizeFitSize.height);
  65. }
  66. + (CGFloat)verticalPaddingBetweenFields {
  67. return 10.0;
  68. }
  69. - (CGSize)sizeThatFits:(CGSize)size {
  70. CGSize fitSize = [super sizeThatFits:size];
  71. CGSize constrainSize = CGSizeMake(size.width, CGFLOAT_MAX);
  72. CGFloat height = fitSize.height;
  73. height += [self.fontNameInput sizeThatFits:constrainSize].height;
  74. height += [[self class] verticalPaddingBetweenFields];
  75. height += [self.pointSizeInput sizeThatFits:constrainSize].height;
  76. return CGSizeMake(fitSize.width, height);
  77. }
  78. #pragma mark -
  79. + (BOOL)supportsObjCType:(const char *)type withCurrentValue:(id)value {
  80. NSParameterAssert(type);
  81. return strcmp(type, FLEXEncodeClass(UIFont)) == 0;
  82. }
  83. @end