Use ʻadjustTextPosition (byCharacterOffset: Int)` to move the cursor in the iOS Keyboard Extension. Perfect for Documents
To move the insertion point in the text input view, use the adjustTextPosition(byCharacterOffset:) method. For example, if you want to implement a forward delete action, move the insertion position forward by one character then delete backwards:
// Move the text insertion position forward 1 character (Move forward one character)
textDocumentProxy.adjustTextPosition(byCharacterOffset: 1)
It says, but it's a ** trap **. I think it's hard to notice, so I'll share it.
If implemented in this way, it will behave like this.
It works well up to "aiueo", but it seems strange from the point of reaching kanji and emoji.
Apparently, this method counts "one character" by the number of characters when ʻutf16 is used. So, let's check the characters in the direction of travel before executing, and then ʻadjustTextPosition
.
func getActualOffset(count: Int)->Int{
if count>0{
if let after = textDocumentProxy.documentContextAfterInput{
if after == ""{
return 1 //If there is a line break before one character, after==""Will be.
}
let left = after.prefix(count)
return left.utf16.count
}else{
return 1
}
}else if count<0{
if let before = textDocumentProxy.documentContextBeforeInput{
let right = before.suffix(-count)
return -right.utf16.count
}else{
return -1
}
}else{
return 0
}
}
let offset = getActualOffset(count: 1) //Get the exact offset
textDocumentProxy.adjustTextPosition(byCharacterOffset: offset) //Run
This works as intended.
It's a pitfall hidden in places that are hard to notice, so please be careful.
Recommended Posts