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

时间:2016-01-11 11:49:43

标签: ios swift nstextcontainer nsparagraphstyle

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

它的工作正常,UNTIL第一次换行设置为

NSParagraphStyle.hyphenationFactor = 2

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

1 个答案:

答案 0 :(得分:0)

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

有关详细信息,您可以看到此question