[英]UITextField, set placeholder after text in ios
步骤1:将文本字段的属性设置为xib
然后设置“ Marisa(名字)”,并将(名字)的颜色更改为灰色。
步骤2:在Textfiled DidBiginEditing
方法中,将textField.text
更改为“ Marisa”。
请参考下面的代码。
只需将以下代码复制粘贴到viewdidload()
textfield.attributedPlaceholder = [[NSAttributedString alloc] initWithString:[NSString stringWithFormat:@"Yourtext%@",Placehodertext] attributes:@{NSForegroundColorAttributeName: color}];
将UITextfield
Delegate分配给self
。
textfield.delegate = self
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
textfield.text = @"Yourtext"
return YES;
}
您不能在文本字段中设置占位符以及文本。 但是,对此有另一种选择。 文本字段具有一种称为“左视图”的视图,您可以将其设置为“只读”视图。 尝试这个 :
UIView *paddingView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
UILabel *textLabel = [[UILabel alloc] initWithFrame:paddingView.bounds];
textLabel.text = @"Marissa";
[paddingView addSubView:textLabel];
textField.leftView = paddingView;
textField.leftViewMode = UITextFieldViewModeAlways;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.