Reputation: 49
Here, str
contains emojis and set cursorPosition is not correct:
let cursorPosition = str.characters.count
let cursorRange = NSRange(location: cursorPosition, length: 0)
textInputView.selectedRange = cursorRange
textInputView.scrollRangeToVisible(cursorRange)
Upvotes: 2
Views: 1318
Reputation: 167
This is an old question, but I'll add some points if I ever run into this problem again:
Nirav D answer works if you just want to go to the end of the string.
This happens because while emojis count as a single character position in strings, they count as more than one position in the textfield as the cursor position uses the utf16 string position. That's why the cursor does not go to the end of the textfield with your code.
If you want to move the cursor to any point other than the end, use the string.utf16 corresponding position.
Upvotes: 2
Reputation: 72440
It is look like you want to put cursor at the last of textView
. Try like this way.
textInputView.becomeFirstResponder()
let cursorPosition = str.utf16.count
let cursorRange = NSRange(location: cursorPosition, length: 0)
textInputView.selectedRange = cursorRange
textInputView.scrollRangeToVisible(cursorRange)
Upvotes: 5