限制特定 UITextfield 的复制、粘贴选项
我的 UIView 包含两个 UITextField。我需要限制一个文本字段的复制、粘贴选项。我不想限制另一个文本字段。
当我使用以下代码时,两个字段都受到复制、粘贴的限制。
-(BOOL)canPerformAction:(SEL)action withSender:(id)sender
{
if ( [UIMenuController sharedMenuController] )
{
[UIMenuController sharedMenuController].menuVisible = NO;
}
return NO;
}
任何人都可以为我提供解决方案来解决我的问题。
My UIView contains Two UITextField.I need to restrict copy,paste option for one textfield.I don't want to restrict that for another.
When i am using the following code,Both the field gets restricted from copy,paste.
-(BOOL)canPerformAction:(SEL)action withSender:(id)sender
{
if ( [UIMenuController sharedMenuController] )
{
[UIMenuController sharedMenuController].menuVisible = NO;
}
return NO;
}
Can any one provide me the solution to solve my problem.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我有一个随机的想法,在文本视图上完美运行。没有理由它不能在文本字段上工作。
我将以下内容添加到我想要限制的文本字段中。
然后分配以下内容对其进行编码。
我现在仍然可以滚动文本视图,但长按或双击现在绝对没有任何作用!
I had a random idea that worked perfectly on a text view. No reason why it wouldn't work on a text field.
I added the following to the text field I wanted to restrict.
Then assigned the following code to it.
I can now still scroll through the textview but a long press or double tap now do absolutely nothing!
苹果的解释:
因此,解决方案是子类化 UITextView 并正确返回。
有关该方法的更多信息请参见此处
Explanantion from Apple:
So, the solution is to subclass the UITextView and return properly.
More information about the method here
以下内容可防止粘贴任何长度超过 1 个字符的字符串。然而,1 个字符长的字符串将通过(可能对某些人有用 - 不需要子类化)。
首先给你的textField一个委托
然后将以下方法添加到你的ViewController
The following prevents any string longer than 1 character to be pasted. String that is 1 character long will however get through (could be useful to some people - doesn't need subclassing).
First give your textField a delegate
Then add the following method to your ViewController
创建 UITextField 的子类。在该子类中,实现
然后将此子类用于您不希望能够复制的字段,并使用常规 UITextField 作为您可以从中复制的字段。
Create a subclass of UITextField. In that subclass, implement
Then use this subclass for the field that you don't want to be able to copy in, and use a regular UITextField for the one that you can copy from.