What you need is the rightView UITextField property. Here is a small category I wrote that helps you set constant prefixes or suffixes to text fields:
@implementation UITextField (Additions) - (void)setPrefixText:(NSString *)prefix { UILabel *label = [[UILabel alloc] initWithFrame:CGRectZero]; [label setBackgroundColor:[UIColor clearColor]]; [label setFont:[UIFont fontWithName:self.font.fontName size:self.font.pointSize]]; [label setTextColor:self.textColor]; [label setAlpha:.5]; [label setText:prefix]; CGSize prefixSize = [prefix sizeWithFont:label.font]; label.frame = CGRectMake(0, 0, prefixSize.width, self.frame.size.height); [self setLeftView:label]; [self setLeftViewMode:UITextFieldViewModeAlways]; [label release]; } - (void)setSuffixText:(NSString *)suffix { UILabel *label = [[UILabel alloc] initWithFrame:CGRectZero]; [label setBackgroundColor:[UIColor clearColor]]; [label setFont:[UIFont fontWithName:self.font.fontName size:self.font.pointSize]]; [label setTextColor:self.textColor]; [label setAlpha:.5]; [label setText:suffix]; CGSize suffixSize = [suffix sizeWithFont:label.font]; label.frame = CGRectMake(0, 0, suffixSize.width, self.frame.size.height); [self setRightView:label]; [self setRightViewMode:UITextFieldViewModeAlways]; [label release]; } @end
By the way: https://stackoverflow.com/search?q=uitextfield+rightview : 4,099 results at the moment.
source share