Chris Brandt
Chris Brandt

Reputation: 948

Add label to UITextField

In question 663830, Isaac asks about adding a button to a text field. Can someone show code to add a label to the .rightView property?

Or, is there some better way to include 'permanent' text in a text field?

This is for a calculator that would include units in the field (mg, kg, etc.) without having to maintain a label outside of the text field. Sort of like permanent placeholder text?

Thanks, Chris

Upvotes: 6

Views: 14662

Answers (3)

Paulo Almeida
Paulo Almeida

Reputation: 2154

I know I might be a little late for the party but I'm sure this can help future programmers facing the same situation.

You might wanna create a category to make it easier to use across multiple applications like this:

UITextField+PaddingLabel.h

#import <UIKit/UIKit.h>

@interface UITextField (PaddingLabel)

-(void) setLeftPaddingText:(NSString*) paddingValue width:(CGFloat) width;

-(void) setRightPaddingText:(NSString*) paddingValue width:(CGFloat) width;

@end

UITextField+PaddingLabel.m

#import "UITextField+PaddingLabel.h"

@implementation UITextField (PaddingLabel)

-(void) setLeftPaddingText:(NSString*) paddingValue width:(CGFloat) width
{
    UILabel *paddingLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, width, self.frame.size.height)];
    paddingLabel.text = paddingValue;
    self.leftView = paddingLabel;
    self.leftViewMode = UITextFieldViewModeAlways;
}

-(void) setRightPaddingText:(NSString*) paddingValue width:(CGFloat) width
{
    UILabel *paddingLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, width, self.frame.size.height)];
    paddingLabel.text = paddingValue;
    self.rightView = paddingLabel;
    self.rightViewMode = UITextFieldViewModeAlways;
}

@end

Usage example:

[self.dateTextField setLeftPaddingText:@"DATE:" width:defaultWidth];

Upvotes: 2

Joshua Dance
Joshua Dance

Reputation: 10482

The leftView (or rightView) property on UITextField is money on this one. Very easy to make 'permanent' text in a text field.

- (void)loadView
{
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];
label.text = @"To:";
label.backgroundColor = [UIColor colorWithWhite:0.0 alpha:0.0];
self.UITextField.leftViewMode = UITextFieldViewModeAlways;
self.UITextField.leftView = label;
}

Upvotes: 3

pgb
pgb

Reputation: 25001

Quick and dirty code:

- (void)viewDidLoad {
    [super viewDidLoad];
    UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(100, 100, 100, 40)];
    textField.borderStyle = UITextBorderStyleLine;
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];
    label.text = @"mg";
    label.backgroundColor = [UIColor colorWithWhite:0.0 alpha:0.0];
    textField.rightViewMode = UITextFieldViewModeAlways;
    textField.rightView = label;
    [self.view addSubview:textField];
    [label release];
    [textField release];
}

Note that I'm adding the subview from the ViewController, you can do it from the view as well.

Upvotes: 11

Related Questions