UITextView disabling text selection
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:
Related videos on Youtube
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, 2022Comments
-
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 about 10 yearsSince 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 over 14 yearsYea I guess. Will be the next thing I do if I don't figure this out.
-
dizy over 14 yearsI mentioned that I already tried that :) canPerform doesn't get called until after you make the selection
-
Ilya Saunkin about 13 yearsthanx dude, I don't know about dizy, but I actually needed a UILabel.
-
Matt Chuang over 12 yearsThis will also disable the scrolling feature on UITextView
-
barfoon over 12 yearsAnd disable the ability to click links
-
Zoltan Varadi over 11 yearsI use the uitextview as a subview in a custom uitableviewcell. It works like a dream
-
Dafydd Williams over 11 yearsIf you don't want any user interaction at all, yes.
-
Albert Renshaw about 11 yearsUILabel 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 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 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 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 over 10 years@MarkAmery you just made my day! Thankyou for the tip, that helps so much with future projects! Haha! :) +1
-
Alexander over 10 yearsHave 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 about 10 yearsThis disables links, etc.
-
Dafydd Williams about 10 yearsA good point. In my case, I wanted links disabled, so I wasn't too fussed about that.
-
Trespassers W almost 10 yearsAnyone 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 about 9 years@TrespassersW overwrite - (void)textViewDidChangeSelection:(UITextView *)textView and set textView.selectedRange = NSMakeRange(0, 0); can accomplish this
-
Krivvenz over 8 yearsThis is what I did for a non editable / selected text field: class
-
Gerard about 8 yearsDownvote: this won't prevent the cut/copy/define toolbar from appearing - there are better solutions listed on this page.
-
He Yifei δ½δΈι over 7 yearsseems like in iOS 10, this code disable selection and keep links & scrolling working at same time
-
bauerMusic about 5 yearsEven 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 about 5 yearsIt seems like there is no setter in objective-C?
-
user963601 about 5 years
-[UITextView isSelectable]
: developer.apple.com/documentation/uikit/uitextview/β¦ -
ArielSD about 5 yearsThat looks like just the
get
- I'm seeing compiler errors when trying to set?textView.isSelectable = NO;
-
user963601 about 5 yearsThat's because
-isSelectable
is the name of the getter only. Use-[UITextView setSelectable:]
to set it. With property syntax, both the getter and setter areselectable
.BOOL foo = textView.selectable;
ortextView.selectable = bar;
-
Mellao over 2 yearsThis one also disables links