我当前有一个带有占位符文本的文本视图,只要用户点击该文本视图,该文本视图就会消失,并且如果第一位响应者辞职,则该文本视图中的文本会再次出现(如果文本视图为空)。 (此处提供了我用于任何人使用的代码)
*注意,首先将textview的文本颜色设置为浅灰色,然后设置占位符文本。然后使用以下方法:
func textViewShouldBeginEditing(_ textView: UITextView) -> Bool {
//If it begins editing, then set the color to black
if (textView.tag == 0){
textView.text = ""
textView.textColor = .black
textView.tag = 1
}
return true
}
func textViewDidEndEditing(_ textView: UITextView) {
if textView.text.isEmpty {
textView.text = "Example: I started my career as a street wear model based in Maryland. After 3 years of working with some of the top companies there, I moved to LA, where I currently reside. I’ve been featured in shows, 12 magazines, commercials, and a number of music videos. Now, Im currently looking to continue working with clothing companies and campaigns."
textView.textColor = .lightGray
textView.tag = 0
}
}
我想加倍努力。现在,只要文本视图成为第一响应者,文本就会消失。 我希望每当用户真正开始输入文字时,文本就会消失,而不仅仅是在选择文本视图时消失。当屏幕出现时,我会自动将第一响应者设置为文本视图,并计划保留该文本那样。但是因为它是自动设置的,所以您看不到占位符文本。 我只希望每当用户按下一个键时文本视图就消失,而不是因为它被选中。
答案 0 :(得分:3)
让我们假设您有一个占位符文本,
let placeholderText = "Example: I started my career as a street wear model based in Maryland. After 3 years of working with some of the top companies there, I moved to LA, where I currently reside. I’ve been featured in shows, 12 magazines, commercials, and a number of music videos. Now, Im currently looking to continue working with clothing companies and campaigns."
在调用textView
的{{1}}之前,您正在storyboard
或viewDidLoad
中将此文本设置为becomeFirstResponder
。
然后在这两个委托方法中,您可以实现以下行为,
textView
当前您正在清除func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
if textView.text == placeholderText {
textView.text = ""
}
return true
}
func textViewDidEndEditing(_ textView: UITextView) {
if textView.text.isEmpty {
textView.text = placeholderText
}
}
中的文本,因此,这是您看不到该文本的主要原因。您应该删除此处的清除文字,但是可以继续更改颜色等。
答案 1 :(得分:1)
这是代码
class ViewController: UIViewController {
@IBOutlet weak var textView: UITextView!
let placeholder = "Example: I started my career as a street wear model based in Maryland. After 3 years of working with some of the top companies there, I moved to LA, where I currently reside. I’ve been featured in shows, 12 magazines, commercials, and a number of music videos. Now, Im currently looking to continue working with clothing companies and campaigns."
let start = NSRange(location: 0, length: 0)
override func viewDidLoad() {
super.viewDidLoad()
textView.delegate = self
textView.text = placeholder
}
}
extension ViewController: UITextViewDelegate {
func textViewDidChangeSelection(_ textView: UITextView) {
// Moves cursor to start when tapped on textView with placeholder
if textView.text == placeholder {
textView.selectedRange = start
}
}
func textViewDidChange(_ textView: UITextView) {
// Manages state of text when changed
if textView.text.isEmpty {
textView.text = placeholder
textView.textColor = .lightGray
} else if textView.text != placeholder {
textView.textColor = .black
}
}
func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
// Called when you're trying to enter a character (to replace the placeholder)
if textView.text == placeholder {
textView.text = ""
}
return true
}
}
更新#1:最大长度
func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
// Called when you're trying to enter a character (to replace the placeholder)
if textView.text == placeholder {
textView.text = ""
} else if textView.text.count >= 175 && text.count > 0 {
// Now it can delete symbols but can't enter new
return false
}
return true
}
尝试通过按住“擦除”按钮清除textView时存在一个小错误。占位符未显示。不会以某种方式调用textViewDidChange方法(与shouldChangeTextIn不同步)。可能的解决方法https://stackoverflow.com/a/9169556/5228431