I hope that user can copy and paste the text but not edit them. I use a delegate UITextField
method to implement this:
我希望用户可以复制和粘贴文本,但不能编辑它们。我使用委托UITextField方法来实现这个:
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
return NO;
}
In this way although the text is selectable and not editable, but when you select the text, the keyboard always shows up, which is a little bit annoying cause you can not edit the text. So is there anyway to make text selectable and not editable, without showing the keyboard?
这样虽然文本是可选择的而且不可编辑,但是当你选择文本时,键盘总是显示出来,这有点烦人,因为你无法编辑文本。那么无论如何在不显示键盘的情况下使文本可选而不可编辑?
5 个解决方案
#1
10
What you need is to allow the control to receive all user interaction events. So, do not return NO
from textFieldShouldBeginEditing
. Instead, do the following:
您需要的是允许控件接收所有用户交互事件。因此,不要从textFieldShouldBeginEditing返回NO。相反,请执行以下操作:
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
return textField != _yourReadOnlyTextField;
}
This will allow the user to select the text and also to select options like Cut
, Copy
and Define
from the popup menu.
这将允许用户选择文本,并从弹出菜单中选择剪切,复制和定义等选项。
UPDATE:
更新:
Also, for completeness, you may want to prevent the keyboard from showing up at all on readyonly textfields. So based on the accepted answer to this question: uitextfield hide keyboard?, you may want to add:
此外,为了完整起见,您可能希望防止键盘在现有文本字段上完全显示。所以基于这个问题的接受答案:uitextfield隐藏键盘?,你可能想要添加:
- (void)viewDidLoad
{
// Prevent keyboard from showing up when editing read-only text field
_yourReadOnlyTextField.inputView = [[UIView alloc] initWithFrame:CGRectZero];
}
#2
1
You should implement another UITextField
's delegate method:
您应该实现另一个UITextField的委托方法:
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
return NO;
}
//UPDATE Also, there is a questions like this here How to disable UITextField's edit property?.
// UPDATE此外,这里有一个类似的问题如何禁用UITextField的编辑属性?
#3
1
Update for Swift users:
Swift用户的更新:
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
return textField != self.yourReadOnlyTextField;
}
and when the view loads
当视图加载时
override func viewDidLoad() {
super.viewDidLoad()
self.selfCodeEdit.inputView = UIView.init();
}
#4
0
if you have more then one textFields you can do in this way
如果你有多个textFields,你可以这样做
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
if textField == firstTextField || textField == secondTextField {
return false
}
return true
}
#5
#1
10
What you need is to allow the control to receive all user interaction events. So, do not return NO
from textFieldShouldBeginEditing
. Instead, do the following:
您需要的是允许控件接收所有用户交互事件。因此,不要从textFieldShouldBeginEditing返回NO。相反,请执行以下操作:
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
return textField != _yourReadOnlyTextField;
}
This will allow the user to select the text and also to select options like Cut
, Copy
and Define
from the popup menu.
这将允许用户选择文本,并从弹出菜单中选择剪切,复制和定义等选项。
UPDATE:
更新:
Also, for completeness, you may want to prevent the keyboard from showing up at all on readyonly textfields. So based on the accepted answer to this question: uitextfield hide keyboard?, you may want to add:
此外,为了完整起见,您可能希望防止键盘在现有文本字段上完全显示。所以基于这个问题的接受答案:uitextfield隐藏键盘?,你可能想要添加:
- (void)viewDidLoad
{
// Prevent keyboard from showing up when editing read-only text field
_yourReadOnlyTextField.inputView = [[UIView alloc] initWithFrame:CGRectZero];
}
#2
1
You should implement another UITextField
's delegate method:
您应该实现另一个UITextField的委托方法:
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
return NO;
}
//UPDATE Also, there is a questions like this here How to disable UITextField's edit property?.
// UPDATE此外,这里有一个类似的问题如何禁用UITextField的编辑属性?
#3
1
Update for Swift users:
Swift用户的更新:
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
return textField != self.yourReadOnlyTextField;
}
and when the view loads
当视图加载时
override func viewDidLoad() {
super.viewDidLoad()
self.selfCodeEdit.inputView = UIView.init();
}
#4
0
if you have more then one textFields you can do in this way
如果你有多个textFields,你可以这样做
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
if textField == firstTextField || textField == secondTextField {
return false
}
return true
}