Reputation: 3171
I do not want the user to be able to select the first few characters of my UITextView
. I have tried subclassing it and noticed methods such as -setSelectedRange:
and -setSelectedTextRange:
. Both are called at different times but it seems like it's the latter that I need.
The -setSelectedTextRange:
method takes a UITextRange
object, which has a UITextPosition
property called "start". This sounds like what I want but I cannot write to it, and there are no classes for this object.
Does anyone have any ideas on how I can do this? FWIW, I'm trying to replicate what Facebook have on their "Check-In" view on their iPhone app.
Thanks in advance!
Upvotes: 0
Views: 1777
Reputation: 357
I'm not personally familiar with the functionality of the Facebook app Check-In view, but based on your description, it sounds like you need something like this in your subclass:
- (BOOL)becomeFirstResponder
{
if ([super becomeFirstResponder]) {
// Select text in field.
[self setSelectedTextRange:[self textRangeFromPosition:[self positionFromPosition:self.beginningOfDocument offset:1] toPosition:self.endOfDocument]];
return YES;
}
return NO;
}
In particular, note the "offset:1" argument. You should be able to use this to set the start of your selected text range. Also, you'll want to make sure that the new text range you specify is valid for the number of characters that are in the text field.
Upvotes: 1