简体   繁体   English

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

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

I'm trying to get an actual point of selected character in NSTextContainer : 我正在尝试获取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 它可以正常工作,直到由TIL设置的第一个换行符

NSParagraphStyle.hyphenationFactor = 2

Every hyphen moves index for one point and glyphIndexForPoint get wrong index. 每个连字符将索引移动一个点,并且glyphIndexForPoint获取错误的索引。 How to get right index with linebreak-mode enabled? 如何在启用换行模式的情况下获取正确的索引?

Try the similar function characterIndexForPoint(_:inTextContainer:fractionOfDistanceBetweenInsertionPoints:) 尝试使用类似的功能characterIndexForPoint(_:inTextContainer:fractionOfDistanceBetweenInsertionPoints :)

For more details, you can see this question 有关更多详细信息,您可以看到此问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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