46

我很难UITextView禁用文本的选择。

我试过了:

canCancelContentTouches = YES;

我试过子类化和覆盖:

- (BOOL)canPerformAction:(SEL)action withSender:(id)sender   

(但只有在选择之后才会调用)

- (BOOL)touchesShouldCancelInContentView:(UIView *)view;  

(我根本看不到会被解雇)

- (BOOL)touchesShouldBegin:(NSSet *)touches
                 withEvent:(UIEvent *)event
             inContentView:(UIView *)view; 

(我也没有看到被解雇)

我错过了什么?

4

11 回答 11

76

问题How disable Copy, Cut, Select, Select All in UITextView有一个可行的解决方案,我刚刚实现并验证了:

子类化UITextView和覆盖canBecomeFirstResponder:

- (BOOL)canBecomeFirstResponder {
    return NO;
}

请注意,这会禁用链接和其他可点击的文本内容。

于 2010-10-28T01:07:44.413 回答
46

我发现打电话

[textView setUserInteractionEnabled:NO];

效果很好。

于 2011-04-17T03:08:57.043 回答
20

UITextViewselectable属性:

此属性控制用户选择内容以及与 URL 和文本附件交互的能力。默认值为是。

于 2016-02-11T20:34:29.013 回答
11

斯威夫特 4,Xcode 10:

如果您想让用户无法选择或编辑文本。

这使得它不能被编辑:

textView.isEditable = false

这将禁用所有用户交互:

textView.isUserInteractionEnabled = false

这使您无法选择它。这意味着它不会显示编辑或粘贴选项。我想这就是你要找的。

textView.isSelectable = false
于 2019-03-01T03:22:52.847 回答
9

听起来你真正想要的是 UIScrollView 中的一个巨大的 UILabel,而不是 UITextView。

更新:如果您使用的是较新版本的 iOS UILabel 现在有一个 lines 属性:

UILabel 中的多行文本

于 2009-10-28T21:04:25.627 回答
8

斯威夫特 4Xcode 10

该解决方案将

  • 禁用突出显示
  • 启用点击链接
  • 允许滚动

确保将委托设置为 YourViewController

yourTextView.delegate = yourViewControllerInstance

然后

extension YourViewController: UITextViewDelegate {

    func textViewDidChangeSelection(_ textView: UITextView) {
        if #available(iOS 13, *) {
            textView.selectedTextRange = nil
        } else {
            view.endEditing(true)
        }
    }

}
于 2018-11-04T18:53:07.120 回答
5

如果您只是想阻止它被编辑,则将 UITextView 的“可编辑”属性设置为 NO/False。

如果您试图让它可编辑但不可选择,那将是棘手的。您可能需要创建一个用户可以输入的隐藏文本视图,然后让 UITextView 观察隐藏的文本视图并使用文本视图的文本填充自身。

于 2009-10-28T19:28:16.257 回答
3

为此,首先子类化 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;
 }

这应该可以正常工作,

于 2017-04-15T13:24:01.253 回答
1

您是否尝试将 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

于 2009-10-28T19:32:22.630 回答
1

您可以通过子类化来禁用文本选择UITextView

下面的解决方案是:

/// 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 {}
    }
}
于 2018-03-22T12:04:10.747 回答
0

对于 swift,有一个名为“isSelectable”的属性,默认情况下分配为 true

您可以按如下方式使用它:

textView.isSelectable = false
于 2018-10-22T09:08:17.217 回答