Can I program text in a UITextView?

I want to select Text on UITextView, similar to the default "Select" and "Select All" options that we see when clicked. I want the user to be able to do this from his user menu. I played with selectedRange, but that doesn't seem to be the trick. Any ideas?

thank

+5
source share
2 answers

The property selectedRangeshould do this, but, as mentioned in the documentation , only on iPhone OS 3.0 and later. In 2.2 and earlier, a property selectedRangeis actually an insertion point.

+5
source

, selectedRange - , , , -textViewDidBeginEditing:, , "":

- (void)textViewDidBeginEditing:(UITextView *)textView
{
    // Look for the default message and highlight it if present
    NSRange defaultMsgRange = [textView.text rangeOfString:NSLocalizedString(@"TEXTFIELD_DEFAULT_MESSAGE", nil) options:NSAnchoredSearch];

    BOOL isDefaultMsg = !(defaultMsgRange.location == NSNotFound && defaultMsgRange.length == 0);
    if (isDefaultMsg) {

        // Need to delay this by one run loop otherwise the insertion wins
        [self performBlock:^(id sender) {  // (BlocksKit - use GCD otherwise)

            textView.selectedRange = defaultMsgRange;

        } afterDelay:0.0];
    }
}
+4

All Articles