0

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:

enter image description here

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.

Jordan H
  • 52,571
  • 37
  • 201
  • 351

1 Answers1

1

These answers should help:UITextField not scrolling horizontally , UITextField functionality with no keyboard

self.pasteboardField.text = [self.pasteboardField.text stringByAppendingString:character];
UITextPosition *end = [self.pasteboardField endOfDocument];
UITextRange *endrange = [self.pasteboardField textRangeFromPosition:end toPosition:end];
[self.pasteboardField setSelectedTextRange:endrange];

UIView *dummyView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 1, 1)];
self.pasteboardField.inputView = dummyView;
[self.pasteboardField becomeFirstResponder];

Above will move cursor to end of textfield without displaying the keyboard, the cursor/caret will be shown though.

self.pasteboardField.tintColor = [UIColor clearColor];

Will hide the cursor/caret

Community
  • 1
  • 1
Ashley
  • 568
  • 2
  • 14
  • I saw that already, but it will not work since sizeWithFont:constrainedToSize:lineBreakMode: is deprecated. Even if I use it, the placeholder text gets aligned right after clearing the field. Also I stated it must work in landscape, and hard-coding a magic number is no good especially if I want to support iPad where the text field will be significantly wider. So I asked my own question hoping for a solution that'll work. :) – Jordan H Apr 08 '14 at 19:08
  • Having a go myself, have edited answer with some extra info – Ashley Apr 08 '14 at 20:14
  • That's quite awesome, nice job! But there's a problem, users need to be able to also enter text into it via keyboard. I added an action upon touchUpInside on the text field to set the inputView back to nil and then becomeFirstResponder, but that method isn't triggered. I tried it on the containing bar button item but that too didn't work. Any ideas? – Jordan H Apr 09 '14 at 01:04
  • I ultimately decided to use the code above with some minor tweaks, then I added code to switch back and forth between the invisible keyboard and the system keyboard in response to what the user does. It's messy but it works. – Jordan H May 18 '14 at 21:12