开发者

UITextView setSelectedRange changes editable property

From the start, my UITextView with editable property to true is not editable (via settings in a NIB). The behaviour is such that a user can select and copy text but not edit. This is the way things should be.

However, if I make a call to setSelectedRange, a side effect is that the editable property is set to YES.

Setting it back to NO [textview setEditable:NO] scrolls to the bottom of the tex开发者_JAVA技巧tView and undoes my programmatic selection. It also doesn't work, as editing becomes enabled anyway. The keyboard appears and everything.

I need to be able to select something programmatically and keep the textView in a state where users can only copy and select text.

[textView select:self];
[textView setSelectedRange:selectedText];

I'm stuck. Looking for any advice you can give.

This doesn't work:

[textView select:self];
[textView setSelectedRange:selectedText];
[textView setEditable:NO];

I've also tried setting the delegate function textViewShouldBeginEditing to return NO:

- (BOOL)textViewShouldBeginEditing:(UITextView *)textView {
    return NO;
}

That just locks everything down and I can't select any text.


I realize this has already been answered, but here is an improvement I made to keep the popout menu, just disable certain options;

-(BOOL) canPerformAction:(SEL)action withSender:(id)sender {
    bool response = [super canPerformAction:action withSender:sender];
    if(response && (action == @selector(cut:) || action == @selector(paste:) || action == @selector(delete:) || action == @selector(_promptForReplace:))) {
        return NO;
    }
    return response;
}

canPerformAction is called per defined action. Calling the parent method will take care of most of these cases, but I have also decided to disable cut,paste,delete and spelling suggestions (_promptForReplace).


This appears to work for me in a similar situation:

  • just let the textview be editable
  • [textView setDelegate:self];
  • [textView select:self];
  • [textView setSelectedRange:range];
  • add a function (BOOL)textView:shouldChangeTextInRange:replacementText: returning NO
  • and the final trick: assign an empty view as a keyboard for the textview, using:

textView.inputView = [[[UIView alloc] initWithFrame:CGRectZero] autorelease];


The textView should be editable:

[textView setEditable:YES];

Do the selection:

[textView select:self];
[textView setSelectedRange:range];

Have these in the delegate:

To disable the menu (not ideal for me but it's ok):

-(BOOL)canPerformAction:(SEL)action withSender:(id)sender
{    
    [UIMenuController sharedMenuController].menuVisible = NO;
    return NO;    
}

To disable the keyboard:

textView.inputView = [[[UIView alloc] initWithFrame:CGRectZero] autorelease];

To disable editing:

- (BOOL)textView:(UITextView*)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString*)text {
    return NO;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜