iOS - 如何使用占位符文本创建 UITextView?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12772413/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 20:38:21  来源:igfitidea点击:

iOS - How do I create a UITextView with placeholder text?

iphoneioscocoa-touchipad

提问by DuckDucking

Possible Duplicate:
Placeholder in UITextView

可能的重复:
UITextView 中的占位符

I am relatively new to iOS and I don't know how to call this in english, but I want to create some UITextViewsthat contain instructions or pre filled text, like "type your name here", or "type your phone", etc, written in light color text but when the user selected the text view to start typing the hints they contain vanish.

我对 iOS 比较陌生,我不知道如何用英语称呼它,但我想创建一些UITextViews包含说明或预填充文本的内容,例如“在此处输入您的姓名”或“输入您的电话”等,以浅色文本书写,但当用户选择文本视图开始输入提示时,它们包含的提示消失。

Is there any example/tutorial etc. I can follow to learn more about it?

是否有任何示例/教程等。我可以关注以了解更多信息?

How do I do that in iOS?

我如何在 iOS 中做到这一点?

回答by shabbirv

There are many ways to do this.

有很多方法可以做到这一点。

Approach #1: UITextView does not have a property for placeholder so you'd have to set the text property to the instructions you would like to have and then as soon as the user taps the UITextView clear it out.

方法#1:UITextView 没有占位符的属性,因此您必须将 text 属性设置为您想要的说明,然后一旦用户点击 UITextView 将其清除。

Approach #2 There is a subclass of UITextViewthat someone made that adds a Placeholder property

方法#2 有一个UITextView子类,它是某人创建的,它添加了一个 Placeholder 属性

回答by Neil

Maybe you are looking for a UITextField.

也许您正在寻找 UITextField。

UITextFields are the more appropriate thing for this method. Then you may use the "placeholder" property to change the greyed-out text.

UITextFields 更适合这种方法。然后您可以使用“占位符”属性来更改灰色文本。