如何用返回键关闭UITextView的键盘?在IB的图书馆里,介绍告诉我们当回归按下键,键盘为UITextView就会消失。但实际上回归键只能充当‘\n’。我可以添加一个按钮[txtView resignFirstResponder]隐藏键盘。但是,是否有一种方法可以将操作添加到回归键盘上的键,这样我就不需要添加UIButton?
3 回答
PIPIONE
TA贡献1829条经验 获得超9个赞
UITextView
UITextField
UITextView
textView:shouldChangeTextInRange:replacementText:
UITextViewDelegate
\n
侃侃无极
TA贡献2051条经验 获得超10个赞
UITextViewDelegate
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text { if([text isEqualToString:@"\n"]) { [textView resignFirstResponder]; return NO; } return YES;}
func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool { if text == "\n" { textView.resignFirstResponder() return false } return true}
FFIVE
TA贡献1797条经验 获得超6个赞
- (BOOL)textView:(UITextView *)txtView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text { if( [text rangeOfCharacterFromSet:[NSCharacterSet newlineCharacterSet]].location == NSNotFound ) { return YES; } [txtView resignFirstResponder]; return NO;}
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}
- 3 回答
- 0 关注
- 341 浏览
添加回答
举报
0/150
提交
取消