[英]How do I program text into textfield?
I am using Xcode 6.3 with Objective-C language. 我正在使用带有Objective-C语言的Xcode 6.3。 The app that I am building has
UITextField
and UITextView
. 我正在构建的应用程序具有
UITextField
和UITextView
。 I am wanting to have text show up in the fields/views so that the user will have instructions on what to put into the fields/views. 我想在字段/视图中显示文本,以便用户对要放入字段/视图的内容进行说明。 Example, "enter your name".
例如,“输入您的名字”。 How can I do this?
我怎样才能做到这一点?
UITextField:
For your UITextField
you should define the placeholder for the instructions text. 对于
UITextField
,应为指令文本定义占位符。
_textField.placeholder= @"Your Name";
UITextView :
UITextView
does not have a default placeholder like UITextField. UITextView
没有像UITextField这样的默认占位符。 But you can make the effect like placeholder by following these steps. 但是,您可以按照以下步骤制作类似占位符的效果。
- (void)viewDidLoad {
[super viewDidLoad];
_textView.text = @"Enter Comment...";
_textView.textColor = [UIColor lightGrayColor];
_textView.delegate = self;
}
- (void)textViewDidBeginEditing:(UITextView *)textView
{
if ([textView.text isEqualToString:@"Enter Comment..."]) {
textView.text = @"";
textView.textColor = [UIColor blackColor];
}
[textView becomeFirstResponder];
}
- (void)textViewDidEndEditing:(UITextView *)textView
{
if ([textView.text isEqualToString:@""]) {
textView.text = @"Enter Comment...";
textView.textColor = [UIColor lightGrayColor];
}
[textView resignFirstResponder];
}
You may be looking for the placeholder
property. 您可能正在寻找
placeholder
属性。
let t = UITextField()
t.placeholder = "enter your name"
文本字段的占位符是显示一些文本的方式...
textField.placeholder = "Enter your name"
if you wann change the text just call: 如果您想更改文本,只需致电:
textfield.text = @"enter your name"
if you just want to set a placeholder which will disappear on user input use this property : 如果您只想设置一个占位符,它将在用户输入时消失,请使用以下属性:
textfield.placeholder = @"enter your name"
if you want a list instructions , please add /place a UIView in appropriate location and show and hide inside your textFieldDidBeginEditing: delegate method. 如果需要列表说明,请在适当的位置添加/放置一个UIView,并在textFieldDidBeginEditing:委托方法中显示和隐藏。
Note :- place holders might not be appropriate as it has limit to text and your instructions may be huge. 注意:-占位符可能不适合,因为它对文本有限制,并且您的说明可能很大。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.