FLEXArgumentInputView.m 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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);
  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)setTitle:(NSString *)title
  33. {
  34. if (![_title isEqual:title]) {
  35. _title = title;
  36. self.titleLabel.text = title;
  37. [self setNeedsLayout];
  38. }
  39. }
  40. - (UILabel *)titleLabel
  41. {
  42. if (!_titleLabel) {
  43. _titleLabel = [[UILabel alloc] init];
  44. _titleLabel.font = [[self class] titleFont];
  45. _titleLabel.backgroundColor = self.backgroundColor;
  46. _titleLabel.textColor = [UIColor colorWithWhite:0.3 alpha:1.0];
  47. _titleLabel.numberOfLines = 0;
  48. [self addSubview:_titleLabel];
  49. }
  50. return _titleLabel;
  51. }
  52. - (BOOL)showsTitle
  53. {
  54. return [self.title length] > 0;
  55. }
  56. #pragma mark - Subclasses Can Override
  57. - (BOOL)inputViewIsFirstResponder
  58. {
  59. return NO;
  60. }
  61. - (void)setInputValue:(id)inputValue
  62. {
  63. // Subclasses should override.
  64. }
  65. - (id)inputValue
  66. {
  67. // Subclasses should override.
  68. return nil;
  69. }
  70. + (BOOL)supportsObjCType:(const char *)type withCurrentValue:(id)value
  71. {
  72. return NO;
  73. }
  74. #pragma mark - Class Helpers
  75. + (UIFont *)titleFont
  76. {
  77. return [FLEXUtility defaultFontOfSize:12.0];
  78. }
  79. + (CGFloat)titleBottomPadding
  80. {
  81. return 4.0;
  82. }
  83. #pragma mark - Sizing
  84. - (CGSize)sizeThatFits:(CGSize)size
  85. {
  86. CGFloat height = 0;
  87. if ([self.title length] > 0) {
  88. CGSize constrainSize = CGSizeMake(size.width, CGFLOAT_MAX);
  89. height += ceil([self.title sizeWithFont:[[self class] titleFont] constrainedToSize:constrainSize].height);
  90. height += [[self class] titleBottomPadding];
  91. }
  92. return CGSizeMake(size.width, height);
  93. }
  94. @end