After adding a tap recognizer to my UITextView
subclass, I'm attempting to get the character that is being tapped:
var textRecognizer: UITapGestureRecognizer!
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
textContainer.lineFragmentPadding = 0
textContainerInset = .zero
textRecognizer = UITapGestureRecognizer(target: self, action: #selector(textTapped))
textRecognizer.numberOfTapsRequired = 1
addGestureRecognizer(textRecognizer)
}
@objc func textTapped(recognizer: UITapGestureRecognizer) {
let location = recognizer.location(in: self)
if let cRange = characterRange(at: location) {
let cPosition = offset(from: beginningOfDocument, to: cRange.start)
let cChar = text[Range(NSRange(location: cPosition, length: 1), in: text)!]
print(cChar)
}
}
Problem is that if my attributedText is "Hello world\nWelcome to Stack Overflow"
and I tap on the left part of a letter, like the left side of letter f
, then characterRange(at: location)
returns the previous letter r
instead of returning f
.
From my perspective,
characterRange(at:)
is buggy:n
, it returns range(n-1, n)
n
, it returns range(n, n+1)
beginningOfDocument
, it returnsnil
endOfDocument
, it returns(endOfDocument, endOfDocument+1)
The discrepancy of the behaviors at the extremities of the textInput demonstrate that there is a bug somewhere.
It behaves like a sort of "cursor position at point" function, which makes it unreliable to determine which character is actually at this point: is it the character before the cursor or the character after the cursor?
closestPosition(to:)
suffers from the exact same issue.A working alternative is
layoutManager.characterIndex(for:in:fractionOfDistanceBetweenInsertionPoints:)
. Credit to vacawama: