ios UITextView 禁用文本选择

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1639485/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 16:34:14  来源:igfitidea点击:

UITextView disabling text selection

iosobjective-ciphoneuitextview

提问by dizy

I'm having a hard time getting the UITextViewto disable the selecting of the text.

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

I've tried:

我试过了:

canCancelContentTouches = YES;

I've tried subclassing and overwriting:

我试过子类化和覆盖:

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

(But that gets called only After the selection)

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

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

(I don't see that getting fired at all)

(我根本没有看到被解雇)

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

(I don't see that getting fired either)

(我也没有看到被解雇)

What am I missing?

我错过了什么?

回答by Dafydd Williams

Issue How disable Copy, Cut, Select, Select All in UITextViewhas a workable solution to this that I've just implemented and verified:

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

Subclass UITextViewand overwrite canBecomeFirstResponder:

子类化UITextView和覆盖canBecomeFirstResponder:

- (BOOL)canBecomeFirstResponder {
    return NO;
}

Note that this disables links and other tappable text content.

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

回答by James M

I've found that calling

我发现调用

[textView setUserInteractionEnabled:NO];

works quite well.

效果很好。

回答by Heath Borders

UITextView's selectableproperty:

UITextViewselectable财产:

This property controls the ability of the user to select content and interact with URLs and text attachments. The default value is YES.

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

回答by slf

It sounds like what you actually want is a giant UILabel inside a UIScrollView, and not a UITextView.

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

update: if you are on newer versions of iOS UILabel now has a lines property:

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

Multiple lines of text in UILabel

UILabel 中的多行文本

回答by TechZen

If you just want to prevent it from being edited, then set the UITextView's "editable" property to NO/False.

如果你只是想阻止它被编辑,那么将 UITextView 的“editable”属性设置为 NO/False。

If you're trying to leave it editable but not selectable, that's going to be tricky. You might need to create a hidden textview that the user can type into and then have UITextView observe that hidden textview and populate itself with the textview's text.

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

回答by Ted

Swift 4, Xcode 10

斯威夫特 4, Xcode 10

This solution will

该解决方案将

  • disable highlighting
  • enable tapping links
  • allow scrolling
  • 禁用突出显示
  • 启用点击链接
  • 允许滚动

Make sure you set the delegate to YourViewController

确保将委托设置为 YourViewController

yourTextView.delegate = yourViewControllerInstance

Then

然后

extension YourViewController: UITextViewDelegate {

    func textViewDidChangeSelection(_ textView: UITextView) {
        view.endEditing(true)
    }

}

回答by aaronmbmorse

Swift 4, Xcode 10:

斯威夫特 4,Xcode 10:

If you want to make it so the user isn't able to select or edit the text.

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

This makes it so it can not be edited:

这使它无法编辑:

textView.isEditable = false

This disables all user interaction:

这将禁用所有用户交互:

textView.isUserInteractionEnabled = false

This makes it so that you can't select it. Meaning it will not show the edit or paste options. I think this is what you are looking for.

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

textView.isSelectable = false

回答by Sajad Goudarzi

To do this first subclass the UITextView

要做到这一点,第一个子类 UITextView

and in the implementation do the following

并在实施中执行以下操作

- (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;
 }

this should work fine,

这应该可以正常工作,

回答by C?ur

You can disable text selection by subclassing UITextView.

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

The below solution is:

下面的解决方案是:

/// 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 = {
        
- (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];
}
.backgroundColor = .clear ##代码##.autoresizingMask = [.flexibleHeight, .flexibleWidth] return ##代码## }(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 {} } }

回答by mahboudz

Did you try setting userInteractionEnabled to NO for your UITextView? But you'd lose scrolling too.

您是否尝试将 UITextView 的 userInteractionEnabled 设置为 NO?但是你也会失去滚动。

If you need scrolling, which is probably why you used a UITextView and not a UILabel, then you need to do more work. You'll probably have to override canPerformAction:withSender:to return NOfor actions that you don't want to allow:

如果您需要滚动,这可能就是您使用 UITextView 而不是 UILabel 的原因,那么您需要做更多的工作。您可能必须重写canPerformAction:withSender:以返回NO您不想允许的操作:

##代码##

For more, UIResponderStandardEditActions .

有关更多信息,UIResponderStandardEditActions