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



我正试图在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:factonOfDistanceBetweenInsertionPoints:)

有关更多详细信息,您可以看到这个问题

最新更新