在IB的库中,介绍告诉我们,当按下返回键时,UITextView的键盘将消失。但实际上返回键只能作为'\n'
我可以添加一个按钮,并使用[txtView resignFirstResponder]隐藏键盘。
但是有没有办法为键盘中的返回键添加动作,这样我就不需要添加UIButton了?
在IB的库中,介绍告诉我们,当按下返回键时,UITextView的键盘将消失。但实际上返回键只能作为'\n'
我可以添加一个按钮,并使用[txtView resignFirstResponder]隐藏键盘。
但是有没有办法为键盘中的返回键添加动作,这样我就不需要添加UIButton了?
当前回答
对于Xcode 6.4。Swift 1.2。:
override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent)
{
super.touchesBegan(touches, withEvent: event)
if let touch = touches.first as? UITouch
{
self.meaningTextview.resignFirstResponder()
}
}
其他回答
用另一种方法解决了这个问题。
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
+ (void)addDoneButtonToControl:(id)txtFieldOrTextView
{
if([txtFieldOrTextView isKindOfClass:[UITextField class]])
{
txtFieldOrTextView = (UITextField *)txtFieldOrTextView;
}
else if([txtFieldOrTextView isKindOfClass:[UITextView class]])
{
txtFieldOrTextView = (UITextView *)txtFieldOrTextView;
}
UIToolbar* numberToolbar = [[UIToolbar alloc]initWithFrame:CGRectMake(0,
0,
[Global returnDeviceWidth],
50)];
numberToolbar.barStyle = UIBarStyleDefault;
UIBarButtonItem *btnDone = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"btn_return"]
style:UIBarButtonItemStyleBordered
target:txtFieldOrTextView
action:@selector(resignFirstResponder)];
numberToolbar.items = [NSArray arrayWithObjects:btnDone,nil];
[numberToolbar sizeToFit];
if([txtFieldOrTextView isKindOfClass:[UITextField class]])
{
((UITextField *)txtFieldOrTextView).inputAccessoryView = numberToolbar;
}
else if([txtFieldOrTextView isKindOfClass:[UITextView class]])
{
((UITextView *)txtFieldOrTextView).inputAccessoryView = numberToolbar;
}
}
试试这个。
NSInteger lengthOfText = [[textView.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] length];
银行的国际代码
在你的类/视图中实现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()
}
}
}
}
编辑 我更新了代码,因为它从来都不是一个好主意,改变用户在文本框的输入,以解决问题,而不是在黑客代码完成后重新设置状态。
函数hideQueboard。
- (void)HideQueyboard
{
[[UIApplication sharedApplication] sendAction:@selector(resignFirstResponder) to:nil from:nil forEvent:nil];
}