alizx alizx - 5 months ago 54
Swift Question

How to convert character index from layoutManager to String scale in swift

How to convert character index from layoutManager to String scale in swift?
this is the code I'm using:

let touchPoint: CGPoint = gesture.locationOfTouch(0, inView: self.definitionLabel)
let index = layoutManager.characterIndexForPoint(touchPoint, inTextContainer: textContainer, fractionOfDistanceBetweenInsertionPoints: nil)


please don't tell me to use
advanceBy()
function on the first index of the string characterset since characters like ò count two in the scale of layoutManager but swift string counts theme once.

Answer

The index returned from the NSLayoutManager is "NSString based", i.e. it is the number of UTF-16 code units from the start of the string to the character at the given point. (So ò actually counts as one, but Emojis

Comments