FLEXArgumentInputFontView.m 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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.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. {
  34. [super setBackgroundColor:backgroundColor];
  35. self.fontNameInput.backgroundColor = backgroundColor;
  36. self.pointSizeInput.backgroundColor = backgroundColor;
  37. }
  38. - (void)setInputValue:(id)inputValue
  39. {
  40. if ([inputValue isKindOfClass:[UIFont class]]) {
  41. UIFont *font = (UIFont *)inputValue;
  42. self.fontNameInput.inputValue = font.fontName;
  43. self.pointSizeInput.inputValue = @(font.pointSize);
  44. }
  45. }
  46. - (id)inputValue
  47. {
  48. CGFloat pointSize = 0;
  49. if ([self.pointSizeInput.inputValue isKindOfClass:[NSValue class]]) {
  50. NSValue *pointSizeValue = (NSValue *)self.pointSizeInput.inputValue;
  51. if (strcmp([pointSizeValue objCType], @encode(CGFloat)) == 0) {
  52. [pointSizeValue getValue:&pointSize];
  53. }
  54. }
  55. return [UIFont fontWithName:self.fontNameInput.inputValue size:pointSize];
  56. }
  57. - (BOOL)inputViewIsFirstResponder
  58. {
  59. return [self.fontNameInput inputViewIsFirstResponder] || [self.pointSizeInput inputViewIsFirstResponder];
  60. }
  61. #pragma mark - Layout and Sizing
  62. - (void)layoutSubviews
  63. {
  64. [super layoutSubviews];
  65. CGFloat runningOriginY = self.topInputFieldVerticalLayoutGuide;
  66. CGSize fontNameFitSize = [self.fontNameInput sizeThatFits:self.bounds.size];
  67. self.fontNameInput.frame = CGRectMake(0, runningOriginY, fontNameFitSize.width, fontNameFitSize.height);
  68. runningOriginY = CGRectGetMaxY(self.fontNameInput.frame) + [[self class] verticalPaddingBetweenFields];
  69. CGSize pointSizeFitSize = [self.pointSizeInput sizeThatFits:self.bounds.size];
  70. self.pointSizeInput.frame = CGRectMake(0, runningOriginY, pointSizeFitSize.width, pointSizeFitSize.height);
  71. }
  72. + (CGFloat)verticalPaddingBetweenFields
  73. {
  74. return 10.0;
  75. }
  76. - (CGSize)sizeThatFits:(CGSize)size
  77. {
  78. CGSize fitSize = [super sizeThatFits:size];
  79. CGSize constrainSize = CGSizeMake(size.width, CGFLOAT_MAX);
  80. CGFloat height = fitSize.height;
  81. height += [self.fontNameInput sizeThatFits:constrainSize].height;
  82. height += [[self class] verticalPaddingBetweenFields];
  83. height += [self.pointSizeInput sizeThatFits:constrainSize].height;
  84. return CGSizeMake(fitSize.width, height);
  85. }
  86. #pragma mark -
  87. + (BOOL)supportsObjCType:(const char *)type withCurrentValue:(id)value
  88. {
  89. NSParameterAssert(type);
  90. return strcmp(type, FLEXEncodeClass(UIFont)) == 0;
  91. }
  92. @end