FLEXArgumentInputTextView.m 5.0 KB

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