FLEXArgumentInputTextView.m 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. self = [super initWithArgumentTypeEncoding:typeEncoding];
  19. if (self) {
  20. self.inputTextView = [UITextView new];
  21. self.inputTextView.font = [[self class] inputFont];
  22. self.inputTextView.backgroundColor = FLEXColor.secondaryGroupedBackgroundColor;
  23. self.inputTextView.layer.cornerRadius = 10.f;
  24. self.inputTextView.contentInset = UIEdgeInsetsMake(0, 5, 0, 0);
  25. self.inputTextView.autocapitalizationType = UITextAutocapitalizationTypeNone;
  26. self.inputTextView.autocorrectionType = UITextAutocorrectionTypeNo;
  27. self.inputTextView.delegate = self;
  28. #if !TARGET_OS_TV
  29. self.inputTextView.inputAccessoryView = [self createToolBar];
  30. #endif
  31. if (@available(iOS 11, *)) {
  32. [self.inputTextView.layer setValue:@YES forKey:@"continuousCorners"];
  33. } else {
  34. self.inputTextView.layer.borderWidth = 1.f;
  35. self.inputTextView.layer.borderColor = FLEXColor.borderColor.CGColor;
  36. }
  37. self.placeholderLabel = [UILabel new];
  38. self.placeholderLabel.font = self.inputTextView.font;
  39. self.placeholderLabel.textColor = FLEXColor.deemphasizedTextColor;
  40. self.placeholderLabel.numberOfLines = 0;
  41. [self addSubview:self.inputTextView];
  42. [self.inputTextView addSubview:self.placeholderLabel];
  43. }
  44. return self;
  45. }
  46. #pragma mark - Private
  47. #if !TARGET_OS_TV
  48. - (UIToolbar *)createToolBar {
  49. UIToolbar *toolBar = [UIToolbar new];
  50. [toolBar sizeToFit];
  51. UIBarButtonItem *spaceItem = [[UIBarButtonItem alloc]
  52. initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
  53. target:nil action:nil
  54. ];
  55. UIBarButtonItem *pasteItem = [[UIBarButtonItem alloc]
  56. initWithTitle:@"Paste" style:UIBarButtonItemStyleDone
  57. target:self.inputTextView action:@selector(paste:)
  58. ];
  59. UIBarButtonItem *doneItem = [[UIBarButtonItem alloc]
  60. initWithBarButtonSystemItem:UIBarButtonSystemItemDone
  61. target:self.inputTextView action:@selector(resignFirstResponder)
  62. ];
  63. toolBar.items = @[spaceItem, pasteItem, doneItem];
  64. return toolBar;
  65. }
  66. #endif
  67. - (void)setInputPlaceholderText:(NSString *)placeholder {
  68. self.placeholderLabel.text = placeholder;
  69. if (placeholder.length) {
  70. if (!self.inputTextView.text.length) {
  71. self.placeholderLabel.hidden = NO;
  72. } else {
  73. self.placeholderLabel.hidden = YES;
  74. }
  75. } else {
  76. self.placeholderLabel.hidden = YES;
  77. }
  78. [self setNeedsLayout];
  79. }
  80. - (NSString *)inputPlaceholderText {
  81. return self.placeholderLabel.text;
  82. }
  83. #pragma mark - Superclass Overrides
  84. - (BOOL)inputViewIsFirstResponder {
  85. return self.inputTextView.isFirstResponder;
  86. }
  87. #pragma mark - Layout and Sizing
  88. - (void)layoutSubviews {
  89. [super layoutSubviews];
  90. self.inputTextView.frame = CGRectMake(0, self.topInputFieldVerticalLayoutGuide, self.bounds.size.width, [self inputTextViewHeight]);
  91. // Placeholder label is positioned by insetting then origin
  92. // by the content inset then the text container inset
  93. CGSize s = self.inputTextView.frame.size;
  94. self.placeholderLabel.frame = CGRectMake(0, 0, s.width, s.height);
  95. self.placeholderLabel.frame = UIEdgeInsetsInsetRect(
  96. UIEdgeInsetsInsetRect(self.placeholderLabel.frame, self.inputTextView.contentInset),
  97. self.inputTextView.textContainerInset
  98. );
  99. }
  100. - (NSUInteger)numberOfInputLines {
  101. switch (self.targetSize) {
  102. case FLEXArgumentInputViewSizeDefault:
  103. return 2;
  104. case FLEXArgumentInputViewSizeSmall:
  105. return 1;
  106. case FLEXArgumentInputViewSizeLarge:
  107. return 8;
  108. }
  109. }
  110. - (CGFloat)inputTextViewHeight {
  111. return ceil([[self class] inputFont].lineHeight * self.numberOfInputLines) + 16.0;
  112. }
  113. - (CGSize)sizeThatFits:(CGSize)size {
  114. CGSize fitSize = [super sizeThatFits:size];
  115. fitSize.height += [self inputTextViewHeight];
  116. return fitSize;
  117. }
  118. #pragma mark - Class Helpers
  119. + (UIFont *)inputFont {
  120. return [UIFont systemFontOfSize:14.0];
  121. }
  122. #pragma mark - UITextViewDelegate
  123. - (void)textViewDidChange:(UITextView *)textView {
  124. [self.delegate argumentInputViewValueDidChange:self];
  125. self.placeholderLabel.hidden = !(self.inputPlaceholderText.length && !textView.text.length);
  126. }
  127. @end