UITextView disabling text selection

后端 未结 11 1093
误落风尘
误落风尘 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 00:52

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

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

    Multiple lines of text in UILabel

    0 讨论(0)
  • 2020-12-08 00:52

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

    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 NO for actions that you don't want to allow:

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

    For more, UIResponderStandardEditActions .

    0 讨论(0)
  • 2020-12-08 00:53

    For swift, there is a property called "isSelectable" and its by default assign to true

    you can use it as follow:

    textView.isSelectable = false
    
    0 讨论(0)
  • 2020-12-08 00:55

    Swift 4, Xcode 10

    This solution will

    • disable highlighting
    • enable tapping links
    • allow scrolling

    Make sure you set the delegate to YourViewController

    yourTextView.delegate = yourViewControllerInstance
    

    Then

    extension YourViewController: UITextViewDelegate {
    
        func textViewDidChangeSelection(_ textView: UITextView) {
            view.endEditing(true)
        }
    
    }
    
    0 讨论(0)
  • 2020-12-08 00:55

    If you just want to prevent it from being edited, then set the UITextView's "editable" property to 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.

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

    Swift 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
    
    0 讨论(0)
提交回复
热议问题