I am programmatically appending text to a UITextField
while the keyboard is not on screen (so there is no typing indicator in the field). Once the text fills up the field, it will continue adding text but will not scroll to the end to show the last character added. Instead, it truncates the text and adds an ellipses (...) at the end.
How can I ensure the last character added programmatically will be visible?
The solution needs to work in landscape (and iPad) when the text field is wider, and users need to be able to also enter text into this field via the on-screen keyboard. [Note that it does auto scroll to the end when the keyboard is up (text field is firstResponder
) and I programmatically append a character.]
Note that this question has been asked before (hence the reason this question is marked as duplicate) but the given solution will not work because sizeWithFont:constrainedToSize:lineBreakMode:
is deprecated. Even if I use it, the placeholder text becomes right-aligned after clearing the field via the Clear (X) button. It also won't work because a magic number is hard-coded so it won't work for different sized text fields.
The answer provided by Ashley is good but also will not work (for me) because users need to be able to also enter text into the field via the system keyboard.
Here's my simple line of code to append a character:
self.textField.text = [self.textField.text stringByAppendingString:character];
Here's what occurs when the text is too long to fit in the visible field:
I wonder if a UITextView
would be better to work with but I imagine I would run into the same situation - would need to scroll to the bottom when it's not going to fit.
I appreciate your help in solving this, and I'm sure those who stumble upon this question will as well.