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.

126 lines
2.7 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) UILabel *titleLabel;
  12. @property (nonatomic) 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 new];
  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. + (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