You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

121 lines
3.8 KiB

  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. #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. {
  19. self = [super initWithArgumentTypeEncoding:typeEncoding];
  20. if (self) {
  21. self.fontNameInput = [[FLEXArgumentInputFontsPickerView alloc] initWithArgumentTypeEncoding:FLEXEncodeClass(NSString)];
  22. self.fontNameInput.backgroundColor = self.backgroundColor;
  23. self.fontNameInput.targetSize = FLEXArgumentInputViewSizeSmall;
  24. self.fontNameInput.title = @"Font Name:";
  25. [self addSubview:self.fontNameInput];
  26. self.pointSizeInput = [FLEXArgumentInputViewFactory argumentInputViewForTypeEncoding:@encode(CGFloat)];
  27. self.pointSizeInput.backgroundColor = self.backgroundColor;
  28. self.pointSizeInput.targetSize = FLEXArgumentInputViewSizeSmall;
  29. self.pointSizeInput.title = @"Point Size:";
  30. [self addSubview:self.pointSizeInput];
  31. }
  32. return self;
  33. }
  34. - (void)setBackgroundColor:(UIColor *)backgroundColor
  35. {
  36. [super setBackgroundColor:backgroundColor];
  37. self.fontNameInput.backgroundColor = backgroundColor;
  38. self.pointSizeInput.backgroundColor = backgroundColor;
  39. }
  40. - (void)setInputValue:(id)inputValue
  41. {
  42. if ([inputValue isKindOfClass:[UIFont class]]) {
  43. UIFont *font = (UIFont *)inputValue;
  44. self.fontNameInput.inputValue = font.fontName;
  45. self.pointSizeInput.inputValue = @(font.pointSize);
  46. }
  47. }
  48. - (id)inputValue
  49. {
  50. CGFloat pointSize = 0;
  51. if ([self.pointSizeInput.inputValue isKindOfClass:[NSValue class]]) {
  52. NSValue *pointSizeValue = (NSValue *)self.pointSizeInput.inputValue;
  53. if (strcmp([pointSizeValue objCType], @encode(CGFloat)) == 0) {
  54. [pointSizeValue getValue:&pointSize];
  55. }
  56. }
  57. return [UIFont fontWithName:self.fontNameInput.inputValue size:pointSize];
  58. }
  59. - (BOOL)inputViewIsFirstResponder
  60. {
  61. return [self.fontNameInput inputViewIsFirstResponder] || [self.pointSizeInput inputViewIsFirstResponder];
  62. }
  63. #pragma mark - Layout and Sizing
  64. - (void)layoutSubviews
  65. {
  66. [super layoutSubviews];
  67. CGFloat runningOriginY = self.topInputFieldVerticalLayoutGuide;
  68. CGSize fontNameFitSize = [self.fontNameInput sizeThatFits:self.bounds.size];
  69. self.fontNameInput.frame = CGRectMake(0, runningOriginY, fontNameFitSize.width, fontNameFitSize.height);
  70. runningOriginY = CGRectGetMaxY(self.fontNameInput.frame) + [[self class] verticalPaddingBetweenFields];
  71. CGSize pointSizeFitSize = [self.pointSizeInput sizeThatFits:self.bounds.size];
  72. self.pointSizeInput.frame = CGRectMake(0, runningOriginY, pointSizeFitSize.width, pointSizeFitSize.height);
  73. }
  74. + (CGFloat)verticalPaddingBetweenFields
  75. {
  76. return 10.0;
  77. }
  78. - (CGSize)sizeThatFits:(CGSize)size
  79. {
  80. CGSize fitSize = [super sizeThatFits:size];
  81. CGSize constrainSize = CGSizeMake(size.width, CGFLOAT_MAX);
  82. CGFloat height = fitSize.height;
  83. height += [self.fontNameInput sizeThatFits:constrainSize].height;
  84. height += [[self class] verticalPaddingBetweenFields];
  85. height += [self.pointSizeInput sizeThatFits:constrainSize].height;
  86. return CGSizeMake(fitSize.width, height);
  87. }
  88. #pragma mark -
  89. + (BOOL)supportsObjCType:(const char *)type withCurrentValue:(id)value
  90. {
  91. BOOL supported = type && strcmp(type, FLEXEncodeClass(UIFont)) == 0;
  92. supported = supported || (value && [value isKindOfClass:[UIFont class]]);
  93. return supported;
  94. }
  95. @end