UITextView disabling text selection

后端 未结 11 1096
误落风尘
误落风尘 2020-12-08 00:19

I\'m having a hard time getting the UITextView to disable the selecting of the text.

I\'ve tried:

canCancelContentTouches = YES;
         


        
相关标签:
11条回答
  • 2020-12-08 01:08

    To do this first subclass the 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,

    0 讨论(0)
  • 2020-12-08 01:11

    You can disable text selection by subclassing UITextView.

    The below solution is:

    • compatible with isScrollEnabled
    • compatible with loupe/magnifier
    • but not compatible with links (see here for a solution compatible with links)
    /// 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 {}
        }
    }
    
    0 讨论(0)
  • 2020-12-08 01:13

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

    Subclass UITextView and overwrite canBecomeFirstResponder:

    - (BOOL)canBecomeFirstResponder {
        return NO;
    }
    

    Note that this disables links and other tappable text content.

    0 讨论(0)
  • 2020-12-08 01:13

    UITextView's selectable property:

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

    0 讨论(0)
  • 2020-12-08 01:17

    I've found that calling

    [textView setUserInteractionEnabled:NO];
    

    works quite well.

    0 讨论(0)
提交回复
热议问题