在IB的库中,介绍告诉我们,当按下返回键时,UITextView的键盘将消失。但实际上返回键只能作为'\n'
我可以添加一个按钮,并使用[txtView resignFirstResponder]隐藏键盘。
但是有没有办法为键盘中的返回键添加动作,这样我就不需要添加UIButton了?
在IB的库中,介绍告诉我们,当按下返回键时,UITextView的键盘将消失。但实际上返回键只能作为'\n'
我可以添加一个按钮,并使用[txtView resignFirstResponder]隐藏键盘。
但是有没有办法为键盘中的返回键添加动作,这样我就不需要添加UIButton了?
当前回答
斯威夫特
func textView(textView: UITextView, shouldChangeTextInRange range: NSRange, replacementText text: String) -> Bool {
if text == "\n" {
textView.resignFirstResponder()
}
return true
}
其他回答
//你可以使用这个…
步骤1。第一步是确保声明了对UITextViewDelegate协议的支持。这是在你的头文件中完成的,例如这里的头文件叫做
EditorController。h:
@interface EditorController : UIViewController {
UITextView *messageTextView;
}
@property (nonatomic, retain) UITextView *messageTextView;
@end
步骤2。接下来你需要将控制器注册为UITextView的委托。从上面的例子继续,这里是我如何用EditorController作为委托初始化UITextView…
- (id) init {
if (self = [super init]) {
// define the area and location for the UITextView
CGRect tfFrame = CGRectMake(10, 10, 300, 100);
messageTextView = [[UITextView alloc] initWithFrame:tfFrame];
// make sure that it is editable
messageTextView.editable = YES;
// add the controller as the delegate
messageTextView.delegate = self;
}
步骤3。现在拼图的最后一块是采取行动响应shouldCahngeTextInRange消息如下:
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range
replacementText:(NSString *)text
{
// Any new character added is passed in as the "text" parameter
if ([text isEqualToString:@"\n"]) {
// Be sure to test for equality using the "isEqualToString" message
[textView resignFirstResponder];
// Return FALSE so that the final '\n' character doesn't get added
return FALSE;
}
// For any other character return TRUE so that the text gets added to the view
return TRUE;
}
-(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"]),你就不能编辑
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
if (range.length==0) {
if ([text isEqualToString:@"\n"]) {
[txtView resignFirstResponder];
if(textView.returnKeyType== UIReturnKeyGo){
[self PreviewLatter];
return NO;
}
return NO;
}
} return YES;
}
一种更优雅的方法是,当用户点击键盘框架之外的某个地方时,取消键盘。
首先,在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];
}
当用户在键盘外点击,并且没有点击输入空间时,键盘将关闭。
+ (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;
}
}