栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

如何在Swift中的UITextView中添加占位符文本?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

如何在Swift中的UITextView中添加占位符文本?

为Swift 4更新

UITextView
本身并不具有占位符属性,因此您必须使用
UITextViewDelegate
方法以编程方式创建和操作一个占位符。我建议根据所需的行为使用下面的解决方案#1或#2。

注意:对于这两种解决方案,请添加

UITextViewDelegate
到类中并设置
textView.delegate =self
为使用文本视图的委托方法。


解决方案#1- 如果您希望占位符在用户选择文本视图后立即消失:

首先将设置

UITextView
为包含占位符文本,然后将其设置为浅灰色,以模仿
UITextField
占位符文本的外观。可以在
viewDidLoad
创建文本视图时或在创建文本视图时执行。

textView.text = "Placeholder"textView.textColor = UIColor.lightGray

然后,当用户开始编辑文本视图时,如果文本视图包含占位符(即,如果其文本颜色为浅灰色),则清除占位符文本并将文本颜色设置为黑色,以适应用户的输入。

func textViewDidBeginEditing(_ textView: UITextView) {    if textView.textColor == UIColor.lightGray {        textView.text = nil        textView.textColor = UIColor.black    }}

然后,当用户完成文本视图的编辑并将其辞去为第一响应者时,如果文本视图为空,请通过重新添加占位符文本并将其颜色设置为浅灰色来重置其占位符。

func textViewDidEndEditing(_ textView: UITextView) {    if textView.text.isEmpty {        textView.text = "Placeholder"        textView.textColor = UIColor.lightGray    }}

解决方案2- 如果希望占位符在文本视图为空时显示,即使选择了文本视图:

首先在中设置占位符

viewDidLoad

textView.text = "Placeholder"textView.textColor = UIColor.lightGraytextView.becomeFirstResponder()textView.selectedTextRange = textView.textRange(from: textView.beginningOfdocument, to: textView.beginningOfdocument)

(注意:由于OP希望在加载视图后立即选择文本视图,因此我将文本视图选择合并到了上面的代码中。如果这不是您想要的行为,并且您不希望在加载视图时选择文本视图,从上面的代码块中删除最后两行。)

然后利用该

shouldChangeTextInRange
UITextViewDelegate
方法,如下所示:

func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {    // Combine the textView text and the replacement text to    // create the updated text string    let currentText:String = textView.text    let updatedText = (currentText as NSString).replacingCharacters(in: range, with: text)    // If updated text view will be empty, add the placeholder    // and set the cursor to the beginning of the text view    if updatedText.isEmpty {        textView.text = "Placeholder"        textView.textColor = UIColor.lightGray        textView.selectedTextRange = textView.textRange(from: textView.beginningOfdocument, to: textView.beginningOfdocument)    }    // Else if the text view's placeholder is showing and the    // length of the replacement string is greater than 0, set     // the text color to black then set its text to the    // replacement string     else if textView.textColor == UIColor.lightGray && !text.isEmpty {        textView.textColor = UIColor.black        textView.text = text    }    // For every other case, the text should change with the usual    // behavior...    else {        return true    }    // ...otherwise return false since the updates have already    // been made    return false}

并且还实现

textViewDidChangeSelection
了防止用户在占位符可见时更改光标位置的工具。(注意:
textViewDidChangeSelection
在视图加载之前被调用,因此如果窗口可见,则仅检查文本视图的颜色):

func textViewDidChangeSelection(_ textView: UITextView) {    if self.view.window != nil {        if textView.textColor == UIColor.lightGray { textView.selectedTextRange = textView.textRange(from: textView.beginningOfdocument, to: textView.beginningOfdocument)        }    }}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/381666.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号