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.

137 lines
2.9 KiB

  1. //
  2. // FLEXArgumentInputView.m
  3. // Flipboard
  4. //
  5. // Created by Ryan Olson on 5/30/14.
  6. // Copyright (c) 2014 Flipboard. All rights reserved.
  7. //
  8. #import "FLEXArgumentInputView.h"
  9. #import "FLEXUtility.h"
  10. @interface FLEXArgumentInputView ()
  11. @property (nonatomic, strong) UILabel *titleLabel;
  12. @property (nonatomic, strong) NSString *typeEncoding;
  13. @end
  14. @implementation FLEXArgumentInputView
  15. - (instancetype)initWithArgumentTypeEncoding:(const char *)typeEncoding
  16. {
  17. self = [super initWithFrame:CGRectZero];
  18. if (self) {
  19. self.typeEncoding = typeEncoding != NULL ? @(typeEncoding) : nil;
  20. }
  21. return self;
  22. }
  23. - (void)layoutSubviews
  24. {
  25. [super layoutSubviews];
  26. if (self.showsTitle) {
  27. CGSize constrainSize = CGSizeMake(self.bounds.size.width, CGFLOAT_MAX);
  28. CGSize labelSize = [self.titleLabel sizeThatFits:constrainSize];
  29. self.titleLabel.frame = CGRectMake(0, 0, labelSize.width, labelSize.height);
  30. }
  31. }
  32. - (void)setBackgroundColor:(UIColor *)backgroundColor
  33. {
  34. [super setBackgroundColor:backgroundColor];
  35. self.titleLabel.backgroundColor = backgroundColor;
  36. }
  37. - (void)setTitle:(NSString *)title
  38. {
  39. if (![_title isEqual:title]) {
  40. _title = title;
  41. self.titleLabel.text = title;
  42. [self setNeedsLayout];
  43. }
  44. }
  45. - (UILabel *)titleLabel
  46. {
  47. if (!_titleLabel) {
  48. _titleLabel = [[UILabel alloc] init];
  49. _titleLabel.font = [[self class] titleFont];
  50. _titleLabel.backgroundColor = self.backgroundColor;
  51. _titleLabel.textColor = [UIColor colorWithWhite:0.3 alpha:1.0];
  52. _titleLabel.numberOfLines = 0;
  53. [self addSubview:_titleLabel];
  54. }
  55. return _titleLabel;
  56. }
  57. - (BOOL)showsTitle
  58. {
  59. return [self.title length] > 0;
  60. }
  61. - (CGFloat)topInputFieldVerticalLayoutGuide
  62. {
  63. CGFloat verticalLayoutGuide = 0;
  64. if (self.showsTitle) {
  65. CGFloat titleHeight = [self.titleLabel sizeThatFits:self.bounds.size].height;
  66. verticalLayoutGuide = titleHeight + [[self class] titleBottomPadding];
  67. }
  68. return verticalLayoutGuide;
  69. }
  70. #pragma mark - Subclasses Can Override
  71. - (BOOL)inputViewIsFirstResponder
  72. {
  73. return NO;
  74. }
  75. - (void)setInputValue:(id)inputValue
  76. {
  77. // Subclasses should override.
  78. }
  79. - (id)inputValue
  80. {
  81. // Subclasses should override.
  82. return nil;
  83. }
  84. + (BOOL)supportsObjCType:(const char *)type withCurrentValue:(id)value
  85. {
  86. return NO;
  87. }
  88. #pragma mark - Class Helpers
  89. + (UIFont *)titleFont
  90. {
  91. return [FLEXUtility defaultFontOfSize:12.0];
  92. }
  93. + (CGFloat)titleBottomPadding
  94. {
  95. return 4.0;
  96. }
  97. #pragma mark - Sizing
  98. - (CGSize)sizeThatFits:(CGSize)size
  99. {
  100. CGFloat height = 0;
  101. if ([self.title length] > 0) {
  102. CGSize constrainSize = CGSizeMake(size.width, CGFLOAT_MAX);
  103. height += ceil([self.titleLabel sizeThatFits:constrainSize].height);
  104. height += [[self class] titleBottomPadding];
  105. }
  106. return CGSizeMake(size.width, height);
  107. }
  108. @end