2015-04-03 120 views
0

如何将默认文本置于文本字段中,该字段在我将焦点输入特定文本字段时应该消失。文本中的默认文本字段

如果我读文本框的大小即textfield.text.size它应该返回我0直到默认文本是存在的。

+1

您可以通过XIB使用占位符。 – user3344236 2015-04-03 09:30:59

+0

如果您不想使用占位符而不是在textfield中设置默认文本并在** textFieldShouldBeginEditing时将其清除**调用委托方法! – 2015-04-03 10:00:57

+0

@SaurabhPrajapati好友在这种情况下,它会采取大于0的文本长度 – 2015-04-03 10:11:52

回答

6

它很简单。你可以使用PlaceHolder Text。属性belongsPlaceholder可从iOS6获得。所以如果您的应用支持iOS5,请不要使用它。

对于Objective C

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

更新:

对于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) 
3

对于默认文本,您可以使用UITextField的属性placeHolder。

@property(nonatomic,copy) NSString *placeholder; 
1

默认文本,可以设置文本占位符文本。

编程方式

[txtField setPlaceholder:@"My placeholder"];

或者你可以保持text as placeholder text从厦门国际银行以及。