По совету @matt, вот функция Swift 4.x:
fileprivate func insertAtTextViewCursor(attributedString: NSAttributedString) {
// Exit if no selected text range
guard let selectedRange = textView.selectedTextRange else {
return
}
// If here, insert <attributedString> at cursor
let cursorIndex = textView.offset(from: textView.beginningOfDocument, to: selectedRange.start)
let mutableAttributedText = NSMutableAttributedString(attributedString: textView.attributedText)
mutableAttributedText.insert(attributedString, at: cursorIndex)
textView.attributedText = mutableAttributedText
}