在启用换行模式的 NSLayoutManager 中获取正确的字形点

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

我正在尝试获取 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)
}

它按预期工作,直到

设置第一个换行符
NSParagraphStyle.hyphenationFactor = 2

每个连字符将索引移动一个点,glyphIndexForPoint 得到错误的索引。如何在启用换行模式的情况下获得正确的索引?

试试类似的功能characterIndexForPoint(_:inTextContainer:fractionOfDistanceBetweenInsertionPoints:)

更多详情可以看这个question