当用户按下 [Return] 键时,需要显示数字。就像每行的序列号 [1,2 等]。有什么办法吗?
我尝试了以下代码
func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
var textview_text_nssring = textView.text as NSString
// Add "1" when the user starts typing into the text field
if (range.location == 0 && textView.text.isEmpty ) {
if text == "\n" {
textView.text = "1."
let cursor = NSMakeRange(range.location + 3, 0)
textView.selectedRange = cursor
return false
}
else {
textView.text = NSString(format: "1. %@", text) as String
}
}
return true
}