Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: selection coordinates on iOS #755

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 25 additions & 20 deletions ios/delegates/KCTextInputCompositeDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,35 @@
// Created by Kiryl Ziusko on 24/04/2024.
//

import CoreText
import Foundation

func textSelection(in textInput: UITextInput) -> NSDictionary? {
if let selectedRange = textInput.selectedTextRange {
let caretRectStart = textInput.caretRect(for: selectedRange.start)
let caretRectEnd = textInput.caretRect(for: selectedRange.end)

return [
"selection": [
"start": [
"x": caretRectStart.origin.x,
"y": caretRectStart.origin.y,
"position": textInput.offset(from: textInput.beginningOfDocument, to: selectedRange.start),
],
"end": [
"x": caretRectEnd.origin.x + caretRectEnd.size.width,
"y": caretRectEnd.origin.y + caretRectEnd.size.height,
"position": textInput.offset(from: textInput.beginningOfDocument, to: selectedRange.end),
],
guard let selectedRange = textInput.selectedTextRange,
let input = textInput as? TextInput,
let font = input.font
else { return nil }

let caretRectStart = textInput.caretRect(for: selectedRange.start)
let caretRectEnd = textInput.caretRect(for: selectedRange.end)

let ctFont = CTFontCreateWithName(font.fontName as CFString, font.pointSize, nil)
let ascent = CTFontGetAscent(ctFont)

return [
"selection": [
"start": [
"x": caretRectStart.origin.x,
"y": caretRectStart.origin.y,
"position": textInput.offset(from: textInput.beginningOfDocument, to: selectedRange.start),
],
]
}

return nil
"end": [
"x": caretRectEnd.origin.x + caretRectEnd.size.width,
"y": caretRectEnd.origin.y + caretRectEnd.size.height + ascent,
"position": textInput.offset(from: textInput.beginningOfDocument, to: selectedRange.end),
],
],
]
}

func updateSelectionPosition(textInput: UITextInput, sendEvent: (_ event: NSDictionary) -> Void) {
Expand Down
1 change: 1 addition & 0 deletions ios/protocols/TextInput.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public protocol TextInput: UIView {
var inputView: UIView? { get set }
var keyboardType: UIKeyboardType { get }
var keyboardAppearance: UIKeyboardAppearance { get }
var font: UIFont? { get }
// custom methods/properties
func focus()
}
Expand Down
Loading