FLEXArgumentInputFontView.m 3.7 KB

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