'How to obtain the offset by (x,y) in TextView when text is SpannableString

I use TextView.layout.getOffsetForHorizontal(line, x) to obtain the offset by x, but it returns a wrong answer when the text is SpannableString, it seems span will be treated as a character when invoke this method, so I can only obtain the offset of the span, not the character offset in the span

val layout = textView.layout ?: return -1
var line = layout.getLineForVertical(y)
var offset = layout.getOffsetForHorizontal(line, x.toFloat())

For example, the text is "i was fine", fine is a span, i can't obtain the offset of 'n'



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source