Problem: while placeholder is shown:
- Paste insert mess inside placeholder with placeholder color
- You can select part of the placeholder and copy it, etc
Saw all suggestions here and got some delegate UITextViewDelegate code:
func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
// if try to paste placeholder - ban
if text == placeholderText {
return false
}
// if paste in placeholder do some trick
if textView.text == placeholderText {
textView.textColor = .label
if text.count > 0 {
textView.text = text
return false
}
textView.text = ""
}
return true
}
// not allow selecting placeholder
func textViewDidChangeSelection(_ textView: UITextView) {
// if they try to select placeholder
if textView.selectedRange.length > 0 && textView.text == placeholderText {
textView.textColor = .label
textView.text = ""
}
}
// restore placeholder when empty
func textViewDidEndEditing(_ textView: UITextView) {
if textView.text.isEmpty {
textView.textColor = .placeholderText
textView.text = placeholderText
}
}
Looks like pinned with nails. Would like to hear any suggestions / improvements