我有一个视图控制器,其中包含两个文本视图(讲故事和故事描述(。我已经将占位符添加到两个文本视图中,下面的代码将向您显示我如何完成的:
// Setup the story title text field
func setupStoryTitleTextView() {
storyTitle.textColor = .gray
storyTitle.text = "Add story title"
storyTitle.textContainerInset = UIEdgeInsets(top: 10, left: 20, bottom: 10, right: 20)
self.storyTitle.delegate = self
view.addSubview(storyTitle)
}
// Setup the storyDescription text view
func setupStoryDescriptionTextView() {
storyDescription.text = "Description of the story"
storyDescription.textColor = .gray
storyDescription.textContainerInset = UIEdgeInsets(top: 10, left: 20, bottom: 300, right: 20)
self.storyDescription.delegate = self
view.addSubview(storyDescription)
}
// To remove placeholder text when the user starts typing
func textViewDidBeginEditing(_ textView: UITextView) {
// Story title text view
if storyTitle.textColor == .gray {
storyTitle.text = nil
storyTitle.textColor = .black
}
// Story description text view
if storyDescription.textColor == .gray {
storyDescription.text = nil
storyDescription.textColor = .black
}
}
// To bring the placeholder back when the text views don't contain any text
func textViewDidEndEditing(_ textView: UITextView) {
// Story title text view
if storyTitle.text.isEmpty {
storyTitle.text = "Add story title"
storyTitle.textColor = .gray
}
// Story description text view
if storyDescription.text.isEmpty {
storyDescription.text = "Description of the story"
storyDescription.textColor = .gray
}
}
这里发生的事情是,当我点击一个文本视图时,两个文本视图中的占位符都消失了,这是因为执行了textViewDidBeginEditing
下的条件。我如何防止这种情况发生?感谢您的任何帮助,谢谢:(
您可以通过分配标签来查看对象(您的textViews(来轻松执行此操作。用 textView.tag = [某些数字]分配标签,然后您也可以添加到类似的条件中:
storyDescription.tag = 1
func textViewDidBeginEditing(_ textView: UITextView) {
if textView.tag == 1 && storyTitle.textColor == .gray {
storyTitle.text = nil
storyTitle.textColor = .black
}
}
和其他TextView的类似物
// To remove placeholder text when the user starts typing
func textViewDidBeginEditing(_ textView: UITextView) {
// Story title text view
if storyTitle.textColor == .gray {
storyTitle.text = nil
storyTitle.textColor = .black
}
// Story description text view
if storyDescription.textColor == .gray {
storyDescription.text = nil
storyDescription.textColor = .black
}
}
// To bring the placeholder back when the text views don't contain any text
func textViewDidEndEditing(_ textView: UITextView) {
// Story title text view
if storyTitle.text.isEmpty {
storyTitle.text = "Add story title"
storyTitle.textColor = .gray
}
// Story description text view
if storyDescription.text.isEmpty {
storyDescription.text = "Description of the story"
storyDescription.textColor = .gray
}
}
此代码将适用于您的两个文本视图。这是错误的,理想情况下,您应该使用代表方法中提供的textView
,例如:
func textViewDidBeginEditing(_ textView: UITextView) {
if textView.textColor == .gray {
textView.text = nil
textView.textColor = .black
}
}
// To bring the placeholder back when the text views don't contain any text
func textViewDidEndEditing(_ textView: UITextView) {
if textView.text.isEmpty {
textView.textColor = .gray
}
// You can then restore the text depending on which TextView ended the edition, for example:
if textView == self.storyTitle {
self.storyTitle.text = "Add story title"
}
}
顺便说一句,决定基于颜色的占位符或不明确显示占位符可能不是最好的方法,尤其是如果您计划最终支持装饰的字符串。这是另一个讨论,但暂时将起作用