When user press [Return] key then need to display the number. Like serial numbers [1,2 etc] for each line.Is there any way to do that?
Following code I tried
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
}
Declare one
Int
var for current line number and use it insideshouldChangeTextIn range
like this way.