Junior Bill gates
Junior Bill gates

Reputation: 1866

Default text in a text Field

How can i Put default text in a textfield which should disappear when i focus particular Text field for entry.

If I read the Size of textfield i.e textfield.text.size it should return me 0 until default text is present.

Upvotes: 1

Views: 3814

Answers (4)

Mayank Patel
Mayank Patel

Reputation: 3908

Its very Simple. you can use PlaceHolder Text. And the property attributedPlaceholder is available from iOS6. So do not use it if your app supports iOS5.

For Objective C

UIColor *color = [UIColor blackColor];
textField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"DemoText" attributes:@{NSForegroundColorAttributeName: color}];

Update:

For swift

var placeholder = NSAttributedString(string: "DemoText", attributes: [NSForegroundColorAttributeName : UIColor.redColor()])
var textField = UITextField(frame: CGRect(x: 0, y: 0, width: 100, height: 30));
textField.attributedPlaceholder = placeholder;
self.view.addSubview(textField)

Upvotes: 7

Jatin Patel - JP
Jatin Patel - JP

Reputation: 3733

For default text, you can set text as placeholder text.

Programatically

[txtField setPlaceholder:@"My placeholder"];

Or you can keep text as placeholder text from xib as well.

Upvotes: 1

generalzyq
generalzyq

Reputation: 313

For the default text, you can use the property placeHolder of UITextField.

@property(nonatomic,copy) NSString *placeholder;  

Upvotes: 3

Pankaj Rathor
Pankaj Rathor

Reputation: 375

Use the placeholder property of UITextField class.

Upvotes: 7

Related Questions