How to adjustTextPosition with iOS Keyboard Extension

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. ezgif-5-786edace6467.gif

It works well up to "aiueo", but it seems strange from the point of reaching kanji and emoji.

Solutions

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. ezgif-5-410ac0a2f5c8.gif

It's a pitfall hidden in places that are hard to notice, so please be careful.

Recommended Posts

How to adjustTextPosition with iOS Keyboard Extension
How to number (number) with html.erb
How to update with activerecord-import
[iOS] How to read Carthage
How to scroll horizontally with ScrollView
How to get started with slim
How to enclose any character with "~"
How to use mssql-tools with alpine
How to get along with Rails
How to start Camunda with Docker
How to crop an image with libGDX
How to share files with Docker Toolbox
How to compile Java with VsCode & Ant
[Java] How to compare with equals method
[Android] How to deal with dark themes
How to use BootStrap with Play Framework
[Rails] How to use rails console with docker
How to switch thumbnail images with JavaScript
[Note] How to get started with Rspec
[IOS] How to get data from DynamoDB
How to do API-based control with cancancan
How to achieve file download with Feign
How to update related models with accepts_nested_attributes_for
How to set JAVA_HOME with Maven appassembler-maven-plugin
How to implement TextInputLayout with validation function
How to handle sign-in errors with devise
How to delete data with foreign key
How to test private scope with JUnit
How to monitor nginx with docker-compose with datadog
Needed for iOS 14? How to set NSUserTrackingUsageDescription
How to deal with Precompiling assets failed.
How to achieve file upload with Feign
How to run Blazor (C #) with Docker
How to build Rails 6 environment with Docker
How to download Oracle JDK 8 rpm with curl
How to mock each case with Mockito 1x
How to mock each case with PowerMock + Mockito1x
How to use MyBatis2 (iBatis) with Spring Boot 1.4 (Spring 4)
How to save to multiple tables with one input
How to test interrupts during Thread.sleep with JUnit
How to use built-in h2db with spring boot
How to search multiple columns with gem ransack
How to use Java framework with AWS Lambda! ??
How to deploy
[Swift] How to link the app with Firebase
How to create multiple pull-down menus with ActiveHash
How to use Java API with lambda expression
How to get started with Eclipse Micro Profile
How to give your image to someone with docker
How to insert all at once with MyBatis
How to monitor SPA site transitions with WKWebView
How to write test code with Basic authentication
[Rails] How to easily implement numbers with pull-down
How to build API with GraphQL and Rails
How to use nfs protocol version 2 with ubuntu 18.04
How to use docker compose with NVIDIA Jetson
How to get resource files out with spring-boot
How to create member variables with JPA Model
How to verify variable items with WireMock's RequestBodyMatching
How to use nginx-ingress-controller with Docker for Mac
[Rails] How to build an environment with Docker