
为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) } }}欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)