Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS 6: UITextField textAlignment does not change cursor position

I use an instance of UITextField in which the text is normally aligned to the right, but switches to left alignment when being edited. This is done by calling setTextAlignment: on the editingDidBegin event. Since the update to iOS 6 this behaves strangely: The text alignment is changed correctly from right to left, but the cursor remains at the far right of the text field until some input is performed.

Does anybody know how to restore the expected behaviour so that the cursor moves as well when the alignment is changed?

To give some context: I use the text field to show a value with a unit. The unit is removed during editing and then displayed again after the user hits enter.

Method called on event editingDidBegin:

- (IBAction)textEditingDidBegin:(UITextField *)sender
{
    [sender setTextAlignment:NSTextAlignmentLeft];
    [sender setText:[NSString stringWithFormat:@"%.0f", width]];
}

Method called on event editingDidEnd:

- (IBAction)textEditingDidEnd:(id)sender
{
    [sender setTextAlignment:UITextAlignmentRight];
    [sender setText:[NSString stringWithFormat:@"%.0f m", width]];
}
like image 543
Klaus Boldt Avatar asked Dec 17 '25 14:12

Klaus Boldt


1 Answers

Try resigning the textView as first responder, then make it first responder right after that.

textView.textAlignment = NSTextAlignmentCenter;
[textView resignFirstResponder];
[textView becomeFirstResponder];
like image 70
Peter Robert Avatar answered Dec 20 '25 07:12

Peter Robert



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!