1

我正在尝试在以下位置获取所选字符的实际点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得到错误的索引。如何在启用换行模式的情况下获得正确的索引?

4

1 回答 1

0

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

有关更多详细信息,您可以查看此问题

于 2016-01-12T07:18:02.010 回答