FLEXArgumentInputView.m 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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) UITextView *inputTextView;
  13. @end
  14. @implementation FLEXArgumentInputView
  15. - (id)initWithFrame:(CGRect)frame
  16. {
  17. self = [super initWithFrame:frame];
  18. if (self) {
  19. // Default to two lines in the text view. Users of the class can customize.
  20. self.numberOfInputLines = 2;
  21. self.inputTextView = [[UITextView alloc] init];
  22. self.inputTextView.font = [[self class] inputFont];
  23. self.inputTextView.backgroundColor = [UIColor whiteColor];
  24. self.inputTextView.layer.borderColor = [[UIColor blackColor] CGColor];
  25. self.inputTextView.layer.borderWidth = 1.0;
  26. self.inputTextView.autocapitalizationType = UITextAutocapitalizationTypeNone;
  27. self.inputTextView.autocorrectionType = UITextAutocorrectionTypeNo;
  28. [self addSubview:self.inputTextView];
  29. }
  30. return self;
  31. }
  32. - (void)layoutSubviews
  33. {
  34. CGFloat originY = 0;
  35. CGFloat contentWidth = self.bounds.size.width;
  36. if ([self.title length] > 0) {
  37. CGSize constrainSize = CGSizeMake(contentWidth, CGFLOAT_MAX);
  38. CGSize labelSize = [self.titleLabel sizeThatFits:constrainSize];
  39. self.titleLabel.frame = CGRectMake(0, originY, labelSize.width, labelSize.height);
  40. originY = CGRectGetMaxY(self.titleLabel.frame) + [[self class] titleBottomPadding];
  41. }
  42. self.inputTextView.frame = CGRectMake(0, originY, contentWidth, [self inputTextViewHeight]);
  43. }
  44. - (void)setTitle:(NSString *)title
  45. {
  46. if (![_title isEqual:title]) {
  47. _title = title;
  48. self.titleLabel.text = title;
  49. [self setNeedsLayout];
  50. }
  51. }
  52. - (UILabel *)titleLabel
  53. {
  54. if (!_titleLabel) {
  55. _titleLabel = [[UILabel alloc] init];
  56. _titleLabel.font = [[self class] titleFont];
  57. _titleLabel.backgroundColor = self.backgroundColor;
  58. _titleLabel.textColor = [UIColor colorWithWhite:0.3 alpha:1.0];
  59. _titleLabel.numberOfLines = 0;
  60. [self addSubview:_titleLabel];
  61. }
  62. return _titleLabel;
  63. }
  64. #pragma mark - Text View Passthroughs
  65. - (void)setKeyboardType:(UIKeyboardType)keyboardType
  66. {
  67. self.inputTextView.keyboardType = keyboardType;
  68. }
  69. - (UIKeyboardType)keyboardType
  70. {
  71. return self.inputTextView.keyboardType;
  72. }
  73. - (void)setInputText:(NSString *)inputText
  74. {
  75. self.inputTextView.text = inputText;
  76. }
  77. - (NSString *)inputText
  78. {
  79. return self.inputTextView.text;
  80. }
  81. - (BOOL)inputViewIsFirstResponder
  82. {
  83. return self.inputTextView.isFirstResponder;
  84. }
  85. #pragma mark - Class Helpers
  86. + (UIFont *)inputFont
  87. {
  88. return [FLEXUtility defaultFontOfSize:14.0];
  89. }
  90. + (UIFont *)titleFont
  91. {
  92. return [FLEXUtility defaultFontOfSize:12.0];
  93. }
  94. + (CGFloat)titleBottomPadding
  95. {
  96. return 4.0;
  97. }
  98. #pragma mark - Sizing
  99. - (CGFloat)inputTextViewHeight
  100. {
  101. return ceil([[self class] inputFont].lineHeight * self.numberOfInputLines) + 20.0;
  102. }
  103. - (CGSize)sizeThatFits:(CGSize)size
  104. {
  105. CGFloat height = 0;
  106. if ([self.title length] > 0) {
  107. CGSize constrainSize = CGSizeMake(size.width, CGFLOAT_MAX);
  108. height += ceil([self.title sizeWithFont:[[self class] titleFont] constrainedToSize:constrainSize].height);
  109. height += [[self class] titleBottomPadding];
  110. }
  111. height += [self inputTextViewHeight];
  112. return CGSizeMake(size.width, height);
  113. }
  114. @end