FLEXArgumentInputView.m 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. #import "FLEXColor.h"
  11. @interface FLEXArgumentInputView ()
  12. @property (nonatomic) UILabel *titleLabel;
  13. @property (nonatomic) NSString *typeEncoding;
  14. @end
  15. @implementation FLEXArgumentInputView
  16. - (instancetype)initWithArgumentTypeEncoding:(const char *)typeEncoding
  17. {
  18. self = [super initWithFrame:CGRectZero];
  19. if (self) {
  20. self.typeEncoding = typeEncoding != NULL ? @(typeEncoding) : nil;
  21. }
  22. return self;
  23. }
  24. - (void)layoutSubviews
  25. {
  26. [super layoutSubviews];
  27. if (self.showsTitle) {
  28. CGSize constrainSize = CGSizeMake(self.bounds.size.width, CGFLOAT_MAX);
  29. CGSize labelSize = [self.titleLabel sizeThatFits:constrainSize];
  30. self.titleLabel.frame = CGRectMake(0, 0, labelSize.width, labelSize.height);
  31. }
  32. }
  33. - (void)setBackgroundColor:(UIColor *)backgroundColor
  34. {
  35. [super setBackgroundColor:backgroundColor];
  36. self.titleLabel.backgroundColor = backgroundColor;
  37. }
  38. - (void)setTitle:(NSString *)title
  39. {
  40. if (![_title isEqual:title]) {
  41. _title = title;
  42. self.titleLabel.text = title;
  43. [self setNeedsLayout];
  44. }
  45. }
  46. - (UILabel *)titleLabel
  47. {
  48. if (!_titleLabel) {
  49. _titleLabel = [UILabel new];
  50. _titleLabel.font = [[self class] titleFont];
  51. _titleLabel.textColor = [FLEXColor primaryTextColor];
  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. + (BOOL)supportsObjCType:(const char *)type withCurrentValue:(id)value
  76. {
  77. return NO;
  78. }
  79. #pragma mark - Class Helpers
  80. + (UIFont *)titleFont
  81. {
  82. return [FLEXUtility defaultFontOfSize:12.0];
  83. }
  84. + (CGFloat)titleBottomPadding
  85. {
  86. return 4.0;
  87. }
  88. #pragma mark - Sizing
  89. - (CGSize)sizeThatFits:(CGSize)size
  90. {
  91. CGFloat height = 0;
  92. if (self.title.length > 0) {
  93. CGSize constrainSize = CGSizeMake(size.width, CGFLOAT_MAX);
  94. height += ceil([self.titleLabel sizeThatFits:constrainSize].height);
  95. height += [[self class] titleBottomPadding];
  96. }
  97. return CGSizeMake(size.width, height);
  98. }
  99. @end