I am looking for a simple answer for this problem...
I have a UITextView
in which the user can start typing and click on DONE and resign the keyboard.
When the wants to edit it again, I want the cursor (the blinking line) to be at the first position of the textView
, not at the end of textView. (act like a placeholder)
I tried setSelectedRange
with NSMakeRange(0,0)
on textViewDidBeginEditing
, but it does not work.
It can be seen that.. when the user taps on the textView
the cursor
comes up at the position where the user taps on the textView
.
I want it to always blink at starting position when textViewDidBeginEditing
.
The property selectedRange can not be assigned at "any place", to make it work you have to implement the method - (void)textViewDidChangeSelection:(UITextView *)textView
, in your case:
- (void)textViewDidChangeSelection:(UITextView *)textView
{
[textView setSelectedRange:NSMakeRange(0, 0)];
}
you will have to detect when the user is beginning editing or selecting text
My solution:
- (void) viewDidLoad {
UITextView *textView = [[UITextView alloc] initWithFrame: CGRectMake(0, 0, 200, 200)];
textView.text = @"This is a test";
[self.view addSubview: textView];
textView.delegate = self;
[textView release];
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget: self action: @selector(tapped:)];
[textView addGestureRecognizer: tap];
[tap release];
}
- (void) tapped: (UITapGestureRecognizer *) tap {
[textView becomeFirstResponder];
}
- (void) textViewDidBeginEditing:(UITextView *)textView {
textView.selectedRange = NSMakeRange(0, 0);
}
I guess it's UITextView internal mechanism to set the cursor when user taps on it. We need to override that by attaching a tap gesture recognizer and call becomeFirstResponder
instead.
I was facing the same issue - basically there's a delay when becoming first responder that doesn't allow you to change selectedRange
in any of textView*BeginEditing:
methods. If you try to delay the setSelectedRange:
(let's say with performSelector:withObject:afterDelay:
) it shows ugly jerk.
The solution is actually pretty simple - checking order of delegate methods gives you the hint:
textViewShouldBeginEditing:
textViewDidBeginEditing:
textViewDidChangeSelection:
Setting selectedRange
in the last method (3) does the trick, you just need to make sure you reposition the cursor only for the first time when the UITextView
becomes first responder as the method (3) is called every time you update the content.
A BOOL variable set in shouldChangeTextInRange:
one of the methods (1), (2) and check for the variable in (3) should do the trick ... just don't forget to reset the variable after the reposition to avoid constant cursor reset :).
Hope it helps!
After few rounds of testing I decided to set the BOOL flag in shouldChangeTextInRange:
instead of (2) or (3) as it proved to be more versatile. See my code:
@interface MyClass
{
/** A flag to determine whether caret should be positioned (YES - don't position caret; NO - move caret to beginning). */
BOOL _isContentGenerated;
}
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
// deleting
if([text length] == 0)
{
// deleting last character
if(range.length == [[textView text] length])
{
// reached beginning
/**
code to show placeholder and reset caret to the beginning
*/
_isContentGenerated = NO;
}
}
else
{
// adding
if(range.location == 0)
{
/**
code to hide placeholder
*/
_isContentGenerated = YES;
}
}
return YES;
}
- (void)textViewDidChangeSelection:(UITextView *)textView
{
if(!_isContentGenerated)
{
[textView setSelectedRange:NSMakeRange(0, 0)];
}
}
I haven't worked enough with that to help you fully, but what happens when you try to play with different selectedRange
s? Say, if you do [... setSelectedRange:[NSMakeRange(0,1)]]
or [... setSelectedRange:[NSMakeRange(1,0)]]
? Does it move the cursor anywhere?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With