双击 uitextview
Posted
技术标签:
【中文标题】双击 uitextview【英文标题】:Double tap uitextview 【发布时间】:2013-12-06 09:48:50 【问题描述】:如果用户双击它,我只想让我的 uitextview 可编辑。
但在我的代码中它只工作一次。隐藏键盘后双击停止 像我想的那样工作。它只显示“复制和定义”弹出窗口,如果我再次尝试...
也许“textViewDidEndEditing”不是正确的方法......
代码如下:
- (void)viewDidLoad
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
CGFloat screen_height = [UIScreen mainScreen].bounds.size.height;
CGFloat screen_width = [UIScreen mainScreen].bounds.size.width;
scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0,0,screen_width,screen_height)];
scrollView.showsVerticalScrollIndicator=NO;
scrollView.scrollEnabled=NO;
scrollView.userInteractionEnabled=YES;
[scrollView setBackgroundColor:[UIColor colorWithRed: 0.0 green: 0.0 blue: 0.0 alpha: 0.5]];
[self.view addSubview:scrollView];
scrollView.contentSize = CGSizeMake(screen_width,screen_height);
MyTextView = [[UITextView alloc]initWithFrame:CGRectMake(0, 0, screen_width, screen_height)];
MyTextView.text = @"Headline\nNo1";
MyTextView.textColor = [UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:1.0];
[MyTextView setFont:[UIFont fontWithName:@"Helvetica" size:90]];
MyTextView.textAlignment = NSTextAlignmentCenter;
MyTextView.delegate = self;
[MyTextView setScrollEnabled:NO];
[MyTextView setEditable:NO];
[MyTextView setUserInteractionEnabled:YES];
[MyTextView setBackgroundColor:[UIColor colorWithRed: 0.0 green: 0.0 blue: 0.0 alpha: 0.0]];
[MyTextView sizeToFit];
[scrollView addSubview:MyTextView];
// TAP
UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(doubleTap:)];
[doubleTap setNumberOfTapsRequired:2];
[doubleTap setNumberOfTouchesRequired:1];
[MyTextView addGestureRecognizer:doubleTap];
// KEYBOARD
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidHide:) name:UIKeyboardDidHideNotification object:nil];
- (void)didReceiveMemoryWarning
[super didReceiveMemoryWarning];
-(void)doubleTap:(UITapGestureRecognizer *)sender
NSLog(@"View: %@", [[sender view] class]);
UITextView *tappedView = (UITextView *)[sender view];
[tappedView setEditable:YES];
[tappedView becomeFirstResponder];
-(BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
if ([gestureRecognizer isKindOfClass:[UITapGestureRecognizer class]])
if (((UITapGestureRecognizer *)gestureRecognizer).numberOfTapsRequired == 2)
return YES;
return YES;
-(BOOL)canBecomeFirstResponder
if (MyTextView.editable == YES)
return YES;
else
return NO;
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGesture
return YES;
-(void)textViewDidEndEditing:(UITextView *)textView
[textView setEditable:NO];
-(void)keyboardDidHide:(NSNotification *)aNotification
NSLog(@"keyboardDidHide");
// [textView setEditable:NO]; // same result like textViewDidEndEditing
【问题讨论】:
【参考方案1】:从方法中删除/评论[textView setEditable:NO];
-(void)textViewDidEndEditing:(UITextView *)textView
。
这是唯一给你带来麻烦的想法。因为停止 UITextView/UItextField 编辑模式的最佳方法是resignFirstReponder
方法调用。
编辑
对于隐藏键盘,在 viewDidLoad
方法中添加以下行
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideKeyboard)];
[self.view addGestureRecognizer:singleTap];
比添加hideKeyboard
如下
-(void) hideKeyboard
[MyTextView resignFirstResponder];
【讨论】:
像魅力一样工作!坦克你!【参考方案2】:如果你在 textview 上方添加一个 clear view 并在 clear view 上添加双击手势,事情会很简单。 当双击被识别时,移除 clear view 并让 textview 成为第一响应者
[clearView removeFromSuperview] ;
[textview becomeFirstResponder] ;
【讨论】:
我会试试的。感谢您的“务实”想法!以上是关于双击 uitextview的主要内容,如果未能解决你的问题,请参考以下文章
UITextView中,能不能单独设置每行文字的字体,颜色,大小