views:

1784

answers:

5

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 ?

A: 

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.

TechZen
I don't want it to be editable, or selectable.
dizy
Do you need it to scroll?
TechZen
Yes, I need it to scroll.
dizy
A: 

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 .

mahboudz
Yea, I'd like to keep scrolling, just disabled selecting/editing
dizy
Ok. See my edit.
mahboudz
I mentioned that I already tried that :) canPerform doesn't get called until after you make the selection
dizy
+2  A: 

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

slf
Yea I guess. Will be the next thing I do if I don't figure this out.
dizy
A: 

I'm having the same problem. Anyone figured how to do this?

Noam
A: 

Issue http://stackoverflow.com/questions/1426731 has a workable solution to this that I've just implemented and verified:

Subclass UITextView and overwrite canBecomeFirstResponder:

- (BOOL)canBecomeFirstResponder {
    return NO;
}
Dafydd Williams