简体   繁体   中英

Get right point of glyph in NSLayoutManager with linebreak-mode enabled

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM