简体   繁体   English

如何以编程方式从textview中解除键盘

[英]How to dismiss a keyboard from textview programmatically

Is there any way to hide or do not show the keyboard without calling resignFirstResponder in iOS? 如果没有在iOS中调用resignFirstResponder ,有没有办法隐藏或不显示键盘? I know different method to hide keyboard, first one is to make the textview non editable, or using 我知道隐藏键盘的不同方法,首先是使textview不可编辑或使用

- (BOOL)textFieldShouldEndEditing:(UITextView *)textview
{
    [textview resignFirstResponder];
}

but in my case these method will not be works out, becz I just want to dismiss or hide the keyboard only, but need the editing in textview. 但在我的情况下,这些方法将无法解决,因为我只想解除或隐藏键盘,但需要在textview中进行编辑。 I have a textview which contains some text, when I tap the sentence it need to be selected, I have done that selection part nicely, but when I write above keyboard dismissal method in my project, the selection of the tapped sentence doesn't appear,.is there any method to o this. 我有一个文本视图,其中包含一些文本,当我点击它需要选择的句子时,我已经很好地完成了选择部分,但是当我在我的项目中编写上面的键盘解雇方法时,没有出现被点击的句子的选择,。这有什么办法吗?

Textfield's selection will remain only when you are in editing mode. 仅当您处于编辑模式时,文本字段的选择才会保留。 When you resign your first responder your selection disappears too. 当您辞职的第一响应者时,您的选择也会消失。

To avoid bringing keypad in editing mode, you can change the input view. 为避免将键盘置于编辑模式,您可以更改输入视图。 For example , look at the code, 例如,看看代码,

    textview.inputView = [[UIView alloc] init];

This brings a custom input view and it is not visible. 这会带来自定义输入视图,但不可见。

Add

self.textView.editable = NO;

Try this 试试这个

NSRange selection = [yourTextView.text rangeOfString:yourTextView.text]; 
if( selection.location != NSNotFound ){ yourTextView.selectedRange = selection; }

It will remove the keyboard from the textview. 它将从textview中删除键盘。 Hope it helps. 希望能帮助到你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM