KBSelectableTextView.m 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. #import "KBSelectableTextView.h"
  2. #import "NSObject+FLEX_Reflection.h"
  3. @interface KBSelectableTextView(){
  4. NSString *_startValue;
  5. NSString *_endValue;
  6. }
  7. @property UITextField *_backingTextField; //i absolutely hate this but i cant figure out how to get a keyboard editing view to present from a UITextView manually.
  8. @end
  9. @implementation KBSelectableTextView
  10. - (BOOL)becomeFirstResponder {
  11. [super becomeFirstResponder];
  12. return [__backingTextField becomeFirstResponder];
  13. }
  14. - (void)textFieldDidBeginEditing:(UITextField *)textField {
  15. _startValue = textField.text;
  16. }
  17. - (void)textFieldDidEndEditing:(UITextField *)textField {
  18. _endValue = textField.text;
  19. if (_startValue != _endValue && _endValue.length > 0){
  20. [self setText:textField.text];
  21. }
  22. }
  23. - (void)_sharedInitialize {
  24. UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap)];
  25. tap.allowedPressTypes = @[[NSNumber numberWithInteger:UIPressTypeSelect]];
  26. [self addGestureRecognizer:tap];
  27. self.selectable = YES;
  28. self.userInteractionEnabled = YES;
  29. self.scrollEnabled = NO;
  30. self.layoutManager.allowsNonContiguousLayout = NO;
  31. self.panGestureRecognizer.allowedTouchTypes = @[@(UITouchTypeIndirect)];
  32. self.focusColorChange = YES;
  33. __backingTextField = [[UITextField alloc] initWithFrame:CGRectZero];
  34. [self addSubview:__backingTextField];
  35. __backingTextField.delegate = self;
  36. __backingTextField.text = self.text;
  37. __backingTextField.placeholder = self.text;
  38. }
  39. - (id)initWithFrame:(CGRect)frame {
  40. self = [super initWithFrame:frame];
  41. [self _sharedInitialize];
  42. return self;
  43. }
  44. - (id)initForAutoLayout
  45. {
  46. self = [super init];
  47. self.translatesAutoresizingMaskIntoConstraints = NO;
  48. [self _sharedInitialize];
  49. return self;
  50. }
  51. - (void)awakeFromNib
  52. {
  53. [super awakeFromNib];
  54. }
  55. - (BOOL)isSelectable
  56. {
  57. return YES;
  58. }
  59. - (BOOL)canBecomeFocused
  60. {
  61. return YES;
  62. }
  63. - (BOOL)canBecomeFirstResponder {
  64. return YES;
  65. }
  66. - (void)didUpdateFocusInContext:(UIFocusUpdateContext *)context withAnimationCoordinator:(UIFocusAnimationCoordinator *)coordinator
  67. {
  68. if (self.focusColorChange == NO) {
  69. [super didUpdateFocusInContext:context withAnimationCoordinator:coordinator];
  70. return;
  71. }
  72. if (context.nextFocusedView == self)
  73. {
  74. [coordinator addCoordinatedAnimations:^{
  75. UIColor *whiteAlpha = [UIColor colorWithWhite:1 alpha:.3];
  76. self.layer.backgroundColor = whiteAlpha.CGColor;
  77. self.layer.shadowOffset = CGSizeMake(5.0, 5.0);
  78. self.layer.shadowRadius = 4;
  79. self.layer.shadowOpacity = 1.0;
  80. self.layer.shadowColor = [UIColor blackColor].CGColor;
  81. } completion:^{
  82. }];
  83. } else {
  84. [coordinator addCoordinatedAnimations:^{
  85. self.layer.backgroundColor = [UIColor clearColor].CGColor;
  86. self.layer.shadowOffset = CGSizeZero;
  87. self.layer.shadowRadius = 0;
  88. self.layer.shadowOpacity = 0;
  89. self.layer.shadowColor = [UIColor clearColor].CGColor;
  90. } completion:^{
  91. }];
  92. }
  93. }
  94. - (void)tap {
  95. NSLog(@"[FLEXLog] tapped");
  96. if(self.inputViewController == nil){
  97. if (self.inputView){
  98. NSLog(@"[FLEXLog] unhandled input view type: %@", self.inputView);
  99. }
  100. [self becomeFirstResponder];
  101. } else {
  102. [[self topViewController] presentViewController:self.inputViewController animated:true completion:nil];
  103. }
  104. }
  105. @end