在IB的库中,介绍告诉我们,当按下返回键时,UITextView的键盘将消失。但实际上返回键只能作为'\n'
我可以添加一个按钮,并使用[txtView resignFirstResponder]隐藏键盘。
但是有没有办法为键盘中的返回键添加动作,这样我就不需要添加UIButton了?
在IB的库中,介绍告诉我们,当按下返回键时,UITextView的键盘将消失。但实际上返回键只能作为'\n'
我可以添加一个按钮,并使用[txtView resignFirstResponder]隐藏键盘。
但是有没有办法为键盘中的返回键添加动作,这样我就不需要添加UIButton了?
当前回答
-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
if([text isEqualToString:@"\n"])
[textView resignFirstResponder];
return YES;
}
yourtextView.delegate=self;
还要添加UITextViewDelegate
别忘了确认协议
如果你没有添加IF ([text isEqualToString:@"\n"]),你就不能编辑
其他回答
用另一种方法解决了这个问题。
Create a button that will be placed in the background From the Attribute Inspector, change the button type to custom, and the makes the button transparent. Expand the button to cover the whole view, and make sure the button is behind all the other object. Easy way to do this is to drag the button to the top of list view in the View Control drag the button to the viewController.h file and create an action (Sent Event: Touch Up Inside) like : (IBAction)ExitKeyboard:(id)sender; In ViewController.m should look like : (IBAction)ExitKeyboard:(id)sender { [self.view endEditing:TRUE]; } Run app, and when you click away from the TextView, the keyboard disappears
我知道这不是这个问题的确切答案,但我在网上寻找答案后找到了这个帖子。我想其他人也有同样的感觉。
这是我的UITapGestureRecognizer的方差,我发现可靠和易于使用-只需设置TextView的委托到ViewController。
而不是ViewDidLoad我添加了UITapGestureRecognizer当TextView成为编辑活动:
-(void)textViewDidBeginEditing:(UITextView *)textView{
_tapRec = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tap:)];
[self.view addGestureRecognizer: _tapRec];
NSLog(@"TextView Did begin");
}
当我在TextView之外点击,视图结束编辑模式和UITapGestureRecognizer删除自己,所以我可以继续与视图中的其他控件进行交互。
-(void)tap:(UITapGestureRecognizer *)tapRec{
[[self view] endEditing: YES];
[self.view removeGestureRecognizer:tapRec];
NSLog(@"Tap recognized, tapRec getting removed");
}
我希望这能有所帮助。这似乎是显而易见的,但我从来没有在网上看到过这个解决方案-我做错了什么吗?
你应该添加UIToolbar到顶部UITextView,而不是使用shouldChangeTextIn
在Swift 4中
let toolbar = UIToolbar(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: 50))
toolbar.barStyle = .default
toolbar.items = [
UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil),
UIBarButtonItem(title: "Done", style: .done, target: self, action: #selector(doneAction))
]
textView.inputAccessoryView = toolbar
@objc func doneAction(){
self.textView.resignFirstResponder()
}
我发现josebama的回答是这个帖子中最完整、最干净的答案。
下面是Swift 4的语法:
func textView(_ textView: UITextView, shouldChangeTextIn _: NSRange, replacementText text: String) -> Bool {
let resultRange = text.rangeOfCharacter(from: CharacterSet.newlines, options: .backwards)
if text.count == 1 && resultRange != nil {
textView.resignFirstResponder()
// Do any additional stuff here
return false
}
return true
}
我知道这已经回答了,但我真的不喜欢使用字符串字面量的换行,所以这是我所做的。
- (BOOL)textView:(UITextView *)txtView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
if( [text rangeOfCharacterFromSet:[NSCharacterSet newlineCharacterSet]].location == NSNotFound ) {
return YES;
}
[txtView resignFirstResponder];
return NO;
}
Swift 4.0更新:
func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
if (text as NSString).rangeOfCharacter(from: CharacterSet.newlines).location == NSNotFound {
return true
}
txtView.resignFirstResponder()
return false
}