简体   繁体   English

如何将文本编程到文本字段中?

[英]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 . 我正在构建的应用程序具有UITextFieldUITextView 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.

相关问题 如何验证文本字段仅显示2种文本 - How do I validate a textfield to only show 2 kinds of text 如何在UIAlertController内的块中使用TextField中的文本? - How do i use the text in TextField in a block inside UIAlertController? 如何从UIView.textField读取文本? - How do I read text from a UIView.textField? 如何更改TextField的文本大小和字段大小? - How do I change text size and field size of a TextField? 如何根据编辑textfield1更新textfield2? - How do I update textfield2 based on editing textfield1? 当我的文本字段为空时,我的应用程序崩溃了,我如何响应一个空的文本字段? - When my textfield is empty, my app crashes how do I respond to an empty text field? 使用`textField:shouldChangeCharactersInRange:`,如何获取包含当前输入字符的文本? - Using `textField:shouldChangeCharactersInRange:`, how do I get the text including the current typed character? 如何在iOS中为类似标签的文本输入创建文本字段? - How do I create a Textfield for Tag-Like-Text Input in iOS? 如何在Swift中将自定义单元格的文本字段的文本设置为自定义对象数组内的变量? - How do I set the text of a custom cell's textfield to a variable inside a custom object array in Swift? 如何使用Xcode UI Automation测试在警报文本字段中输入文本 - How do I enter text in an alert textfield using Xcode UI Automation testing
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM