Ais*_*rya 106 objective-c uitextview ios
该UITextView
的复制,剪切,选择,全选功能在默认情况下,当我按下屏幕上显示.但是,在我的项目中,UITextField
它只是只读.我不需要这个功能.请告诉我如何禁用此功能.
rpe*_*ich 103
禁用粘贴板操作的最简单方法是创建一个子类,UITextView
该子类重写该canPerformAction:withSender:
方法以返回NO
您不想允许的操作:
- (BOOL)canPerformAction:(SEL)action withSender:(id)sender
{
if (action == @selector(paste:))
return NO;
return [super canPerformAction:action withSender:sender];
}
Run Code Online (Sandbox Code Playgroud)
小智 67
子类UITextView并覆盖canBecomeFirstResponder:
- (BOOL)canBecomeFirstResponder {
return NO;
}
Run Code Online (Sandbox Code Playgroud)
请注意,这仅适用于不可编辑的UITextViews!没有在可编辑的上测试它...
Dam*_*bin 29
如果要在所有 UITextView
应用程序上禁用剪切/复制/粘贴,可以使用以下类别:
@implementation UITextView (DisableCopyPaste)
- (BOOL)canBecomeFirstResponder
{
return NO;
}
@end
Run Code Online (Sandbox Code Playgroud)
它保存了子类...... :-)
小智 29
这对我来说是最好的解决方案:
UIView *overlay = [[UIView alloc] init];
[overlay setFrame:CGRectMake(0, 0, myTextView.contentSize.width, myTextView.contentSize.height)];
[myTextView addSubview:overlay];
[overlay release];
Run Code Online (Sandbox Code Playgroud)
来自:https://stackoverflow.com/a/5704584/1293949
Luk*_*ath 20
如果您不需要UITextView滚动,那么不涉及子类的最简单的解决方案是简单地禁用文本视图的用户交互:
textField.userInteractionEnabled = NO;
Run Code Online (Sandbox Code Playgroud)
pin*_*nch 18
@rpetrich的回答对我有用.我发布扩展代码,以防它节省一些时间.
在我的情况下,我不想要任何弹出窗口,但我确实希望UITextField能够成为第一响应者.
不幸的是,当您点按并按住文本字段时,您仍会获得放大镜弹出窗口.
@interface NoSelectTextField : UITextField
@end
@implementation NoSelectTextField
- (BOOL)canPerformAction:(SEL)action withSender:(id)sender {
if (action == @selector(paste:) ||
action == @selector(cut:) ||
action == @selector(copy:) ||
action == @selector(select:) ||
action == @selector(selectAll:) ||
action == @selector(delete:) ||
action == @selector(makeTextWritingDirectionLeftToRight:) ||
action == @selector(makeTextWritingDirectionRightToLeft:) ||
action == @selector(toggleBoldface:) ||
action == @selector(toggleItalics:) ||
action == @selector(toggleUnderline:)
) {
return NO;
}
return [super canPerformAction:action withSender:sender];
}
@end
Run Code Online (Sandbox Code Playgroud)
斯威夫特4
class NoSelectTextField: UITextField {
override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
if action == #selector(paste(_:)) ||
action == #selector(cut(_:)) ||
action == #selector(copy(_:)) ||
action == #selector(select(_:)) ||
action == #selector(selectAll(_:)) ||
action == #selector(delete(_:)) ||
action == #selector(makeTextWritingDirectionLeftToRight(_:)) ||
action == #selector(makeTextWritingDirectionRightToLeft(_:)) ||
action == #selector(toggleBoldface(_:)) ||
action == #selector(toggleItalics(_:)) ||
action == #selector(toggleUnderline(_:)) {
return false
}
return super.canPerformAction(action, withSender: sender)
}
}
Run Code Online (Sandbox Code Playgroud)
小智 15
最简单的方法是创建一个覆盖canPerformAction的UITextView子类:withSender:
- (BOOL)canPerformAction:(SEL)action withSender:(id)sender
{
[UIMenuController sharedMenuController].menuVisible = NO; //do not display the menu
[self resignFirstResponder]; //do not allow the user to selected anything
return NO;
}
Run Code Online (Sandbox Code Playgroud)
Ada*_*ner 13
当我在iOS 7上的canPerformAction中返回NO时,我会收到很多这样的错误:
<Error>: CGContextSetFillColorWithColor: invalid context 0x0. This is a serious error. This application, or a library it uses, is using an invalid context and is thereby contributing to an overall degradation of system stability and reliability. This notice is a courtesy: please fix this problem. It will become a fatal error in an upcoming update.
我的解决方案如下:
- (BOOL)canPerformAction:(SEL)action withSender:(id)sender {
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
[[UIMenuController sharedMenuController] setMenuVisible:NO animated:NO];
}];
return [super canPerformAction:action withSender:sender];
}
Run Code Online (Sandbox Code Playgroud)
诀窍是在主队列的下一个循环中隐藏菜单控制器(就在它显示之后).
GL7*_*777 10
这是在UITextView中禁用整个选择/复制/粘贴菜单的最简单方法
-(BOOL)canPerformAction:(SEL)action withSender:(id)sender
{
[UIMenuController sharedMenuController].menuVisible = NO;
return NO;
}
Run Code Online (Sandbox Code Playgroud)
您可以通过取消选中这些框来修复故事板中的此问题:
或者您可以像这样以编程方式设置:
textView.selectable = false
textView.editable = false
Run Code Online (Sandbox Code Playgroud)
小智 5
如果您想用 a 替换键盘,比如说,UIPicker
作为inputView
(当然还有一个工具栏作为inputAccesotyView
),那么这个解决方法可能会有所帮助......
textFieldShouldBeginEditing:
textField.userInteractionEnabled = NO;
UIPickerView
,将其设置为 YES。通过这样做,您将能够点击UITextField
并显示从 中进行选择的选项UIPickerView
,此时您UITextField
确实不会对任何触摸事件做出反应(这包括触摸并按住以进行剪切、复制和粘贴)。但是,您必须记住在关闭时将其设置回“是”,UIPickerView
但是您将无法UIPickerView
再次访问。
唯一失败的时刻是当用户通过点击并按住 开始时UITextView
,您会第一次再次看到剪切复制和粘贴。这就是为什么您应该始终验证您的输入。这是我能想到的最简单的方法。另一种选择是将 aUILabel
用于只读文本,但您错过了UITextView
.
子类 UITextView - swift 4.0
override public func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
return false
}
Run Code Online (Sandbox Code Playgroud)
如果您想禁用弹出窗口,请UITextField
尝试使用此UITextFieldDelegate
方法切换isUserInteractionEnabled
.
func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
textField.isUserInteractionEnabled = false
return true
}
func textFieldShouldEndEditing(_ textField: UITextField) -> Bool {
textField.isUserInteractionEnabled = true
return true
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
90339 次 |
最近记录: |