FLEXArgumentInputView.m 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. //
  2. // FLEXArgumentInputView.m
  3. // Flipboard
  4. //
  5. // Created by Ryan Olson on 5/30/14.
  6. // Copyright (c) 2020 FLEX Team. 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. self = [super initWithFrame:CGRectZero];
  18. if (self) {
  19. self.typeEncoding = typeEncoding != NULL ? @(typeEncoding) : nil;
  20. }
  21. return self;
  22. }
  23. - (void)layoutSubviews {
  24. [super layoutSubviews];
  25. if (self.showsTitle) {
  26. CGSize constrainSize = CGSizeMake(self.bounds.size.width, CGFLOAT_MAX);
  27. CGSize labelSize = [self.titleLabel sizeThatFits:constrainSize];
  28. self.titleLabel.frame = CGRectMake(0, 0, labelSize.width, labelSize.height);
  29. }
  30. }
  31. - (void)setBackgroundColor:(UIColor *)backgroundColor {
  32. [super setBackgroundColor:backgroundColor];
  33. self.titleLabel.backgroundColor = backgroundColor;
  34. }
  35. - (void)setTitle:(NSString *)title {
  36. if (![_title isEqual:title]) {
  37. _title = title;
  38. self.titleLabel.text = title;
  39. [self setNeedsLayout];
  40. }
  41. }
  42. - (UILabel *)titleLabel {
  43. if (!_titleLabel) {
  44. _titleLabel = [UILabel new];
  45. _titleLabel.font = [[self class] titleFont];
  46. _titleLabel.textColor = FLEXColor.primaryTextColor;
  47. _titleLabel.numberOfLines = 0;
  48. [self addSubview:_titleLabel];
  49. }
  50. return _titleLabel;
  51. }
  52. - (BOOL)showsTitle {
  53. return self.title.length > 0;
  54. }
  55. - (CGFloat)topInputFieldVerticalLayoutGuide {
  56. CGFloat verticalLayoutGuide = 0;
  57. if (self.showsTitle) {
  58. CGFloat titleHeight = [self.titleLabel sizeThatFits:self.bounds.size].height;
  59. verticalLayoutGuide = titleHeight + [[self class] titleBottomPadding];
  60. }
  61. return verticalLayoutGuide;
  62. }
  63. #pragma mark - Subclasses Can Override
  64. - (BOOL)inputViewIsFirstResponder {
  65. return NO;
  66. }
  67. + (BOOL)supportsObjCType:(const char *)type withCurrentValue:(id)value {
  68. return NO;
  69. }
  70. #pragma mark - Class Helpers
  71. + (UIFont *)titleFont {
  72. return [UIFont systemFontOfSize:12.0];
  73. }
  74. + (CGFloat)titleBottomPadding {
  75. return 4.0;
  76. }
  77. #pragma mark - Sizing
  78. - (CGSize)sizeThatFits:(CGSize)size {
  79. CGFloat height = 0;
  80. if (self.title.length > 0) {
  81. CGSize constrainSize = CGSizeMake(size.width, CGFLOAT_MAX);
  82. height += ceil([self.titleLabel sizeThatFits:constrainSize].height);
  83. height += [[self class] titleBottomPadding];
  84. }
  85. return CGSizeMake(size.width, height);
  86. }
  87. @end