FLEXArgumentInputTextView.m 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. //
  2. // FLEXArgumentInputTextView.m
  3. // FLEXInjected
  4. //
  5. // Created by Ryan Olson on 6/15/14.
  6. //
  7. //
  8. #import "FLEXColor.h"
  9. #import "FLEXArgumentInputTextView.h"
  10. #import "FLEXUtility.h"
  11. @interface FLEXArgumentInputTextView ()
  12. @property (nonatomic) UITextView *inputTextView;
  13. @property (nonatomic) UILabel *placeholderLabel;
  14. @property (nonatomic, readonly) NSUInteger numberOfInputLines;
  15. @end
  16. @implementation FLEXArgumentInputTextView
  17. - (instancetype)initWithArgumentTypeEncoding:(const char *)typeEncoding
  18. {
  19. self = [super initWithArgumentTypeEncoding:typeEncoding];
  20. if (self) {
  21. self.inputTextView = [UITextView new];
  22. self.inputTextView.font = [[self class] inputFont];
  23. self.inputTextView.backgroundColor = [FLEXColor primaryBackgroundColor];
  24. self.inputTextView.layer.borderColor = [FLEXColor borderColor].CGColor;
  25. self.inputTextView.layer.borderWidth = 1.f;
  26. self.inputTextView.layer.cornerRadius = 5.f;
  27. self.inputTextView.autocapitalizationType = UITextAutocapitalizationTypeNone;
  28. self.inputTextView.autocorrectionType = UITextAutocorrectionTypeNo;
  29. self.inputTextView.delegate = self;
  30. self.inputTextView.inputAccessoryView = [self createToolBar];
  31. [self addSubview:self.inputTextView];
  32. self.placeholderLabel = [UILabel new];
  33. self.placeholderLabel.font = self.inputTextView.font;
  34. self.placeholderLabel.textColor = [FLEXColor deemphasizedTextColor];
  35. self.placeholderLabel.numberOfLines = 0;
  36. [self.inputTextView addSubview:self.placeholderLabel];
  37. }
  38. return self;
  39. }
  40. #pragma mark - Private
  41. - (UIToolbar *)createToolBar
  42. {
  43. UIToolbar *toolBar = [UIToolbar new];
  44. [toolBar sizeToFit];
  45. UIBarButtonItem *spaceItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
  46. UIBarButtonItem *doneItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(textViewDone)];
  47. toolBar.items = @[spaceItem, doneItem];
  48. return toolBar;
  49. }
  50. - (void)textViewDone
  51. {
  52. [self.inputTextView resignFirstResponder];
  53. }
  54. - (void)setInputPlaceholderText:(NSString *)placeholder
  55. {
  56. self.placeholderLabel.text = placeholder;
  57. if (placeholder.length) {
  58. if (!self.inputTextView.text.length) {
  59. self.placeholderLabel.hidden = NO;
  60. } else {
  61. self.placeholderLabel.hidden = YES;
  62. }
  63. } else {
  64. self.placeholderLabel.hidden = YES;
  65. }
  66. [self setNeedsLayout];
  67. }
  68. - (NSString *)inputPlaceholderText
  69. {
  70. return self.placeholderLabel.text;
  71. }
  72. #pragma mark - Superclass Overrides
  73. - (BOOL)inputViewIsFirstResponder
  74. {
  75. return self.inputTextView.isFirstResponder;
  76. }
  77. #pragma mark - Layout and Sizing
  78. - (void)layoutSubviews
  79. {
  80. [super layoutSubviews];
  81. self.inputTextView.frame = CGRectMake(0, self.topInputFieldVerticalLayoutGuide, self.bounds.size.width, [self inputTextViewHeight]);
  82. // Placeholder label is positioned by insetting origin,
  83. // which is the line fragment padding for X and 0 for Y,
  84. // by the content inset then the text container inset
  85. CGFloat leading = self.inputTextView.textContainer.lineFragmentPadding;
  86. CGSize s = self.inputTextView.frame.size;
  87. self.placeholderLabel.frame = CGRectMake(leading, 0, s.width, s.height);
  88. self.placeholderLabel.frame = UIEdgeInsetsInsetRect(
  89. UIEdgeInsetsInsetRect(self.placeholderLabel.frame, self.inputTextView.contentInset),
  90. self.inputTextView.textContainerInset
  91. );
  92. }
  93. - (NSUInteger)numberOfInputLines
  94. {
  95. switch (self.targetSize) {
  96. case FLEXArgumentInputViewSizeDefault:
  97. return 2;
  98. case FLEXArgumentInputViewSizeSmall:
  99. return 1;
  100. case FLEXArgumentInputViewSizeLarge:
  101. return 8;
  102. }
  103. }
  104. - (CGFloat)inputTextViewHeight
  105. {
  106. return ceil([[self class] inputFont].lineHeight * self.numberOfInputLines) + 16.0;
  107. }
  108. - (CGSize)sizeThatFits:(CGSize)size
  109. {
  110. CGSize fitSize = [super sizeThatFits:size];
  111. fitSize.height += [self inputTextViewHeight];
  112. return fitSize;
  113. }
  114. #pragma mark - Trait collection changes
  115. - (void)traitCollectionDidChange:(UITraitCollection *)previousTraitCollection
  116. {
  117. #if FLEX_AT_LEAST_IOS13_SDK
  118. if (@available(iOS 13.0, *)) {
  119. if (previousTraitCollection.userInterfaceStyle != self.traitCollection.userInterfaceStyle) {
  120. self.inputTextView.layer.borderColor = [FLEXColor borderColor].CGColor;
  121. }
  122. }
  123. #endif
  124. }
  125. #pragma mark - Class Helpers
  126. + (UIFont *)inputFont
  127. {
  128. return [FLEXUtility defaultFontOfSize:14.0];
  129. }
  130. #pragma mark - UITextViewDelegate
  131. - (void)textViewDidChange:(UITextView *)textView
  132. {
  133. [self.delegate argumentInputViewValueDidChange:self];
  134. self.placeholderLabel.hidden = !(self.inputPlaceholderText.length && !textView.text.length);
  135. }
  136. @end