func textViewDidChangeSelection(_ textView: UITextView) {
let location = textView.selectedRange.location
let length = textView.selectedRange.length
let text = (textView.text as NSString).substring(with: NSRange(location:location,length: length))
print(location)
print(length)
print(text)
}
参照
【swift】uitextviewの選択範囲の文字列を取得する方法
How to restrict cursor position in swift?