在IB的库中,介绍告诉我们,当按下返回键时,UITextView的键盘将消失。但实际上返回键只能作为'\n'
我可以添加一个按钮,并使用[txtView resignFirstResponder]隐藏键盘。
但是有没有办法为键盘中的返回键添加动作,这样我就不需要添加UIButton了?
在IB的库中,介绍告诉我们,当按下返回键时,UITextView的键盘将消失。但实际上返回键只能作为'\n'
我可以添加一个按钮,并使用[txtView resignFirstResponder]隐藏键盘。
但是有没有办法为键盘中的返回键添加动作,这样我就不需要添加UIButton了?
当前回答
UITextView没有任何在用户点击返回键时被调用的方法。如果您希望用户只能添加一行文本,请使用UITextField。按回车键并隐藏UITextView的键盘不符合接口准则。
即使这样,如果你想这样做,实现textView:shouldChangeTextInRange:replacementText:方法的UITextViewDelegate,并在检查替换文本是否\n,隐藏键盘。
也许还有别的办法,但我不知道有什么办法。
其他回答
我想我应该把这个片段放在这里:
确保声明了对UITextViewDelegate协议的支持。
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
if([text isEqualToString:@"\n"]) {
[textView resignFirstResponder];
return NO;
}
return YES;
}
Swift 4.0更新:
func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
if text == "\n" {
textView.resignFirstResponder()
return false
}
return true
}
用另一种方法解决了这个问题。
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
一种更优雅的方法是,当用户点击键盘框架之外的某个地方时,取消键盘。
首先,在UIBuilder的标识检查器中将你的ViewController的视图设置为类“UIControl”。control -拖动视图到ViewController的头文件中,并将其链接为Touch Up Inside事件的动作,例如:
ViewController。h
-(IBAction)dismissKeyboardOnTap:(id)sender;
在ViewController主文件中,ViewController.m:
-(IBAction)dismissKeyboardOnTap:(id)sender
{
[[self view] endEditing:YES];
}
你可以使用类似的技术要求双击或长触。你可能需要设置你的ViewController为一个UITextViewDelegate,并将TextView连接到ViewController。这个方法对UITextView和UITextField都有效。
来源:Big Nerd Ranch
编辑:我还想补充一点,如果你正在使用UIScrollView,上述技术可能不容易通过接口构建器工作。在这种情况下,你可以使用一个UIGestureRecognizer并调用其中的[[self view] enditing:YES]方法。一个例子是:
-(void)ViewDidLoad{
....
UITapGestureRecognizer *tapRec = [[UITapGestureRecognizer alloc]
initWithTarget:self action:@selector(tap:)];
[self.view addGestureRecognizer: tapRec];
....
}
-(void)tap:(UITapGestureRecognizer *)tapRec{
[[self view] endEditing: YES];
}
当用户在键盘外点击,并且没有点击输入空间时,键盘将关闭。
在使用uitextview时还有另一个解决方案, 你可以添加工具栏作为InputAccessoryView在“textViewShouldBeginEditing”,并从这个工具栏的完成按钮,你可以解雇键盘,这是如下代码:
在viewDidLoad
toolBar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 44)]; //toolbar is uitoolbar object
toolBar.barStyle = UIBarStyleBlackOpaque;
UIBarButtonItem *btnDone = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(btnClickedDone:)];
[toolBar setItems:[NSArray arrayWithObject:btnDone]];
在textviewdelegate方法
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView
{
[textView setInputAccessoryView:toolBar];
return YES;
}
在操作按钮完成,这是在工具栏如下:
-(IBAction)btnClickedDone:(id)sender
{
[self.view endEditing:YES];
}
银行的国际代码
在你的类/视图中实现UITextViewDelegate,如下所示:
class MyClass: UITextViewDelegate { ...
设置textView委托为self
myTextView.delegate = self
然后实现以下内容:
func textViewDidChange(_ textView: UITextView) {
if textView.text.characters.count >= 1 {
if let lastChar = textView.text.characters.last {
if(lastChar == "\n"){
textView.text = textView.text.substring(to: textView.text.index(before: textView.text.endIndex))
textView.resignFirstResponder()
}
}
}
}
编辑 我更新了代码,因为它从来都不是一个好主意,改变用户在文本框的输入,以解决问题,而不是在黑客代码完成后重新设置状态。