I have a UITextField that I'd like to add a "?" suffix to all text entered.
The user should not be able to remove this "?" or add text to the right hand side of it.
What's the best way to go about this?
I have a UITextField that I'd like to add a "?" suffix to all text entered.
The user should not be able to remove this "?" or add text to the right hand side of it.
What's the best way to go about this?
For a single-line UITextField you should be able to measure the size of the NSString (it has a measurement function in there, somewhere) and move a UILabel to the right position.
OK, im definitly too late, but maybe i can help someone out either way: The intended way to accomplish this is by using a custom NSFormatter. Heres the docs: https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSFormatter_Class/Reference/Reference.html
The basic idea is this: you create a subclass of NSFormatter, and the override at least the two worker methods:
this will produce the dipsplay-String from the value stored in the object (i.e. add your questionmark here)
here, you need to transform the display-string into an object you want to store, i.e. remove the questionmark
The formatter can then be used to convert the data from the stored objects into strings that are suitable for presentation to the user. The big advantage is that you can use formatters wherever your string will appear in the UI. It is not limited to certain UI-Elements like the solution where you override -drawText in UITextField. Its just hella convenient.
Use the
UITextFieldDelegate
protocol to alter the string whenever the field is being edited. Here's a quick stab at it; this will need work, but it should get you started.I realize this answer is late, but I found most of these did not work for my scenario. I have a UITextField that I simply want to force to have a suffix that the user cannot edit. However, I don't want to subclass UITextView, modify how it handles drawing, etc. I just want to prevent the user from modifying the suffix.
First, I ensure the suffix is set in the textfield when editing takes place. This could be done any number of ways depending upon your scenario. For mine, I wanted it there from the start, so I simply set the textfield's text property equal to the suffix when the view loads and store off the length of the suffix for later. For example:
Then I used the UITextFieldDelegate protocol as Josh suggested above, but use the length of the string and the range to ensure nothing edits the suffix:
This should work for any suffix value you assign to the textfield.
This class method I have written in Objective-C, helps you to add a suffix text to a
UITextField
. I order to make it work, you need to initialize theUILabel
to the prefix or suffix in yourUITextFieldLabel
as follow:Once we have the
UILabel
attached to theUITextField
, you can use this class method to update the text, and this text will be automatically resized to fit in the field.I have written the following method to achieve the above task of placing non-editable suffix to UITextField: