FLEXArgumentInputFontView.m 3.8 KB

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