UITextView 禁用文本选择
-
08-07-2019 - |
题
我很难得到 UITextView
禁用文本的选择。
我试过了:
canCancelContentTouches = YES;
我尝试过子类化和覆盖:
- (BOOL)canPerformAction:(SEL)action withSender:(id)sender
(但只有在选择之后才会被调用)
- (BOOL)touchesShouldCancelInContentView:(UIView *)view;
(我根本不认为会被解雇)
- (BOOL)touchesShouldBegin:(NSSet *)touches
withEvent:(UIEvent *)event
inContentView:(UIView *)view;
(我也不认为会被解雇)
我缺少什么?
解决方案
问题如何在UITextView中禁用复制,剪切,选择,全部选择有一个可行的解决方案,我刚刚实施并验证:
子类 UITextView
并覆盖 canBecomeFirstResponder:
- (BOOL)canBecomeFirstResponder {
return NO;
}
请注意,这会禁用链接和其他可插入文本内容。
其他提示
我发现打电话了
[textView setUserInteractionEnabled:NO];
效果很好。
UITextView
的 selectable
property:
此属性控制用户选择内容的能力 与URL和文本附件交互。默认值为YES。
如果您只是想阻止它被编辑,那么设置UITextView的“可编辑”按钮。财产到NO / False。
如果你试图让它可编辑但不可选择,那将会很棘手。您可能需要创建一个用户可以键入的隐藏文本视图,然后让UITextView观察隐藏的textview并使用textview的文本填充自己。
要做到这一点,首先要继承UITextView
并在实现中执行以下操作
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(nullable UIEvent *)event
{
self.selectable = NO;
}
- (void)touchesCancelled:(nullable NSSet<UITouch *> *)touches withEvent:(nullable UIEvent *)event
{
self.selectable = YES;
}
这应该可以正常工作,
雨燕4, Xcode 10
该解决方案将
- 禁用突出显示
- 启用点击链接
- 允许滚动
确保将委托设置为 YourViewController
yourTextView.delegate = yourViewControllerInstance
然后
extension YourViewController: UITextViewDelegate {
func textViewDidChangeSelection(_ textView: UITextView) {
view.endEditing(true)
}
}
Swift 4,Xcode 10:
如果您想这样做,用户将无法选择或编辑文字。
这使它无法编辑:
textView.isEditable = false
这会禁用所有用户互动:
textView.isUserInteractionEnabled = false
这使得您无法选择它。这意味着它不会显示编辑或粘贴选项。我认为这就是你要找的东西。
textView.isSelectable = false
您是否尝试为您的UITextView设置userInteractionEnabled为NO?但是你也会失去滚动。
如果你需要滚动,这可能是你使用UITextView而不是UILabel的原因,那么你需要做更多的工作。您可能必须覆盖 canPerformAction:withSender:
以返回 NO
以执行您不想允许的操作:
- (BOOL)canPerformAction:(SEL)action withSender:(id)sender
{
switch (action) {
case @selector(paste:):
case @selector(copy:):
case @selector(cut:):
case @selector(cut:):
case @selector(select:):
case @selector(selectAll:):
return NO;
}
return [super canPerformAction:action withSender:sender];
}
更多信息, UIResponderStandardEditActions 。
您可以通过子类化来禁用文本选择 UITextView
.
下面的解决方案是:
- 与 isScrollEnabled 兼容
- 与放大镜兼容
- 但与链接不兼容(请参阅此处以获取与链接兼容的解决方案)
/// Class to disallow text selection
/// while keeping support for loupe/magnifier and scrolling
/// https://stackoverflow.com/a/49428248/1033581
class UnselectableTextView: UITextView {
override init(frame: CGRect, textContainer: NSTextContainer?) {
super.init(frame: frame, textContainer: textContainer)
commonInit()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
commonInit()
}
private func commonInit() {
// prevents selection from loupe/magnifier (_UITextSelectionForceGesture), multi tap, tap and a half, etc.
// without losing the loupe/magnifier or scrolling
// but we lose taps on links
addSubview(transparentOverlayView)
}
let transparentOverlayView: UIView = {
$0.backgroundColor = .clear
$0.autoresizingMask = [.flexibleHeight, .flexibleWidth]
return $0
}(UIView())
override var contentSize: CGSize {
didSet {
transparentOverlayView.frame = CGRect(origin: .zero, size: contentSize)
}
}
// required to prevent blue background selection from any situation
override var selectedTextRange: UITextRange? {
get { return nil }
set {}
}
}
对于swift,有一个名为“isSelectable”的属性。并且默认情况下分配给true
您可以按照以下方式使用它:
textView.isSelectable = false