UITextView disabling text selection

50,013

Solution 1

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.

Solution 2

I've found that calling

[textView setUserInteractionEnabled:NO];

works quite well.

Solution 3

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.

Solution 4

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

Solution 5

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

Share:
50,013

Related videos on Youtube

dizy
Author by

dizy

a.b.c.d.fishes m.n.o.fishes s.a.r.fishes e.d.b.d.fishes o.i.c.d.fishes

Updated on February 21, 2022

Comments

  • dizy
    dizy about 2 years

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

    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?

    • Epaga
      Epaga about 10 years
      Since it's impossible to add answers to this: note the BEST answer here (IMHO) is actually a comment by Alexander: since iOS 7 there is a @property(nonatomic,getter=isSelectable) BOOL selectable NS_AVAILABLE_IOS(7_0)
  • dizy
    dizy over 14 years
    Yea I guess. Will be the next thing I do if I don't figure this out.
  • dizy
    dizy over 14 years
    I mentioned that I already tried that :) canPerform doesn't get called until after you make the selection
  • Ilya Saunkin
    Ilya Saunkin about 13 years
    thanx dude, I don't know about dizy, but I actually needed a UILabel.
  • Matt Chuang
    Matt Chuang over 12 years
    This will also disable the scrolling feature on UITextView
  • barfoon
    barfoon over 12 years
    And disable the ability to click links
  • Zoltan Varadi
    Zoltan Varadi over 11 years
    I use the uitextview as a subview in a custom uitableviewcell. It works like a dream
  • Dafydd Williams
    Dafydd Williams over 11 years
    If you don't want any user interaction at all, yes.
  • Albert Renshaw
    Albert Renshaw about 11 years
    UILabel will prevent the line-breaks. You want a UITextView with no user interaction enabled... and then you want that inside of a UIScrollView. Alternatively, you could display a transparent UIWebView with your text in a textView in the UIWebView (just use loadHTML to import your text/code into the UIWebView (including the CSS code to make the background transparent) then you can use javascript to make it not selectable but still scrollable).
  • Mark Amery
    Mark Amery over 10 years
    @AlbertRenshaw I don't think your first sentence is correct. A quick Google seems to indicate that UILabel supports line breaks just fine.
  • Albert Renshaw
    Albert Renshaw over 10 years
    @MarkAmery Wow! I never knew! UILabel has a "lines" property that is on "1" by default... so if you put in multiple lines nothing will happen, you have to modify the "lines" property to a higher number to support multiple lines! Neat :)
  • Mark Amery
    Mark Amery over 10 years
    @AlbertRenshaw Don't make it higher - set it to 0 instead, and then you can have as many lines as you like.
  • Albert Renshaw
    Albert Renshaw over 10 years
    @MarkAmery you just made my day! Thankyou for the tip, that helps so much with future projects! Haha! :) +1
  • Alexander
    Alexander over 10 years
    Have you tried to use the property selectable? @property(nonatomic,getter=isSelectable) BOOL selectable NS_AVAILABLE_IOS(7_0); // toggle selectability, which controls the ability of the user to select content and interact with URLs & attachments
  • junglecat
    junglecat about 10 years
    This disables links, etc.
  • Dafydd Williams
    Dafydd Williams about 10 years
    A good point. In my case, I wanted links disabled, so I wasn't too fussed about that.
  • Trespassers W
    Trespassers W almost 10 years
    Anyone know of a solution that does not disable links but does disable highlighting? I tried overriding long-press but that seems to disable link tapping as well...
  • Pepsin
    Pepsin about 9 years
    @TrespassersW overwrite - (void)textViewDidChangeSelection:(UITextView *)textView and set textView.selectedRange = NSMakeRange(0, 0); can accomplish this
  • Krivvenz
    Krivvenz over 8 years
    This is what I did for a non editable / selected text field: class
  • Gerard
    Gerard about 8 years
    Downvote: this won't prevent the cut/copy/define toolbar from appearing - there are better solutions listed on this page.
  • He Yifei δ½•δΈ€ιž
    He Yifei δ½•δΈ€ιž over 7 years
    seems like in iOS 10, this code disable selection and keep links & scrolling working at same time
  • bauerMusic
    bauerMusic about 5 years
    Even when disabling selection, user can still long-press and 'drag away' the url. In cases where this was meant to act as a button (and the URL, private), this behavior sure hammers in the final nail.
  • ArielSD
    ArielSD about 5 years
    It seems like there is no setter in objective-C?
  • user963601
    user963601 about 5 years
  • ArielSD
    ArielSD about 5 years
    That looks like just the get - I'm seeing compiler errors when trying to set? textView.isSelectable = NO;
  • user963601
    user963601 about 5 years
    That's because -isSelectable is the name of the getter only. Use -[UITextView setSelectable:] to set it. With property syntax, both the getter and setter are selectable. BOOL foo = textView.selectable; or textView.selectable = bar;
  • Mellao
    Mellao over 2 years
    This one also disables links