Is there a built-in way to add a character prefix to a UITextField
like the screenshot below?
If no, what is the best, most straight-forward way to accomplish this? I am thinking the background
property might be able to do this.
An object that displays an editable text area in your interface.
If you have a UITextField or UITextView and want to stop users typing in more than a certain number of letters, you need to set yourself as the delegate for the control then implement either shouldChangeCharactersIn (for text fields) or shouldChangeTextIn (for text views).
Following on from Anne's code you can also do this directly in code without the UILabel in IB and UIView in code:
UILabel *poundLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 5, 20)];
poundLabel.text = @"£";
[self.paymentAmount setLeftView:poundLabel];
[self.paymentAmount setLeftViewMode:UITextFieldViewModeAlways];
Simply adding a gray UILabel
on top of the UITextField
does the trick:
Note that the UILabel
behaves like a background image, the entered text stays on top:
UPDATE
Additionally you can add some padding to the UITextField
using the following code:
UIView *thePadding = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 5, 20)];
theTextField.leftView = thePadding;
theTextField.leftViewMode = UITextFieldViewModeAlways;
This way the text cannot cover the prefix character.
Adjust the rect to make it working properly in your situation.
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