I'm trying to get an actual point of selected character in NSTextContainer
:
func handleTouch(gestureRecognizer: UIGestureRecognizer) {
var location = gestureRecognizer.locationInView(self)
let startPoint = self.layoutManager.locationForGlyphAtIndex(0)
location = CGPoint(x: location.x - startPoint.x, y: location.y - startPoint.y)
var fraction: CGFloat = 0
let index = self.layoutManager.glyphIndexForPoint(location, inTextContainer: textContainer, fractionOfDistanceThroughGlyph: &fraction)
}
And it works works as it should, UNTIL first linebreak set by
NSParagraphStyle.hyphenationFactor = 2
Every hyphen moves index for one point and glyphIndexForPoint
get wrong index. How to get right index with linebreak-mode enabled?
Try the similar function characterIndexForPoint(_:inTextContainer:fractionOfDistanceBetweenInsertionPoints:)
For more details, you can see this question