I\'m having a hard time getting the UITextView
to disable the selecting of the text.
I\'ve tried:
canCancelContentTouches = YES;
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,
You can disable text selection by subclassing 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 = {
$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 {}
}
}
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.
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.
I've found that calling
[textView setUserInteractionEnabled:NO];
works quite well.