You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

121 lines
3.1 KiB

  1. //
  2. // FLEXArgumentInputTextView.m
  3. // FLEXInjected
  4. //
  5. // Created by Ryan Olson on 6/15/14.
  6. //
  7. //
  8. #import "FLEXArgumentInputTextView.h"
  9. #import "FLEXUtility.h"
  10. @interface FLEXArgumentInputTextView () <UITextViewDelegate>
  11. @property (nonatomic, strong) UITextView *inputTextView;
  12. @property (nonatomic, readonly) NSUInteger numberOfInputLines;
  13. @end
  14. @implementation FLEXArgumentInputTextView
  15. - (instancetype)initWithArgumentTypeEncoding:(const char *)typeEncoding
  16. {
  17. self = [super initWithArgumentTypeEncoding:typeEncoding];
  18. if (self) {
  19. self.inputTextView = [[UITextView alloc] init];
  20. self.inputTextView.font = [[self class] inputFont];
  21. self.inputTextView.backgroundColor = [UIColor whiteColor];
  22. self.inputTextView.layer.borderColor = [[UIColor blackColor] CGColor];
  23. self.inputTextView.layer.borderWidth = 1.0;
  24. self.inputTextView.autocapitalizationType = UITextAutocapitalizationTypeNone;
  25. self.inputTextView.autocorrectionType = UITextAutocorrectionTypeNo;
  26. self.inputTextView.delegate = self;
  27. self.inputTextView.inputAccessoryView = [self createToolBar];
  28. [self addSubview:self.inputTextView];
  29. }
  30. return self;
  31. }
  32. #pragma mark - private
  33. - (UIToolbar*)createToolBar
  34. {
  35. UIToolbar *toolBar = [UIToolbar new];
  36. [toolBar sizeToFit];
  37. UIBarButtonItem *spaceItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
  38. UIBarButtonItem *doneItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(textViewDone)];
  39. toolBar.items = @[spaceItem, doneItem];
  40. return toolBar;
  41. }
  42. - (void)textViewDone
  43. {
  44. [self.inputTextView resignFirstResponder];
  45. }
  46. #pragma mark - Text View Changes
  47. - (void)textViewDidChange:(UITextView *)textView
  48. {
  49. [self.delegate argumentInputViewValueDidChange:self];
  50. }
  51. #pragma mark - Superclass Overrides
  52. - (BOOL)inputViewIsFirstResponder
  53. {
  54. return self.inputTextView.isFirstResponder;
  55. }
  56. #pragma mark - Layout and Sizing
  57. - (void)layoutSubviews
  58. {
  59. [super layoutSubviews];
  60. self.inputTextView.frame = CGRectMake(0, self.topInputFieldVerticalLayoutGuide, self.bounds.size.width, [self inputTextViewHeight]);
  61. }
  62. - (NSUInteger)numberOfInputLines
  63. {
  64. NSUInteger numberOfInputLines = 0;
  65. switch (self.targetSize) {
  66. case FLEXArgumentInputViewSizeDefault:
  67. numberOfInputLines = 2;
  68. break;
  69. case FLEXArgumentInputViewSizeSmall:
  70. numberOfInputLines = 1;
  71. break;
  72. case FLEXArgumentInputViewSizeLarge:
  73. numberOfInputLines = 8;
  74. break;
  75. }
  76. return numberOfInputLines;
  77. }
  78. - (CGFloat)inputTextViewHeight
  79. {
  80. return ceil([[self class] inputFont].lineHeight * self.numberOfInputLines) + 16.0;
  81. }
  82. - (CGSize)sizeThatFits:(CGSize)size
  83. {
  84. CGSize fitSize = [super sizeThatFits:size];
  85. fitSize.height += [self inputTextViewHeight];
  86. return fitSize;
  87. }
  88. #pragma mark - Class Helpers
  89. + (UIFont *)inputFont
  90. {
  91. return [FLEXUtility defaultFontOfSize:14.0];
  92. }
  93. @end