如何获取 NSLayoutManager
中的字形总数?
我正在制作一个光标层以覆盖在自定义 TextView 上,如下所示:
func moveCursorToGlyphIndex(glyphIndex: Int) {
// get bounding rect for glyph
var rect = self.layoutManager.boundingRectForGlyphRange(NSRange(location: glyphIndex, length: 1), inTextContainer: view.textContainer)
rect.origin.y = self.textContainerInset.top
rect.size.width = cursorWidth
// set cursor layer frame to right edge of rect
cursorLayer.frame = rect
}
但是,我想确保 glyphIndex
没有越界。像这样:
if glyphIndex < self.layoutManager.glyphCount { // doesn't work
// ...
}
不过,我无法通过反复试验找到合适的属性,在线搜索也没有显示任何 SO 答案。
我终于在文档中找到了它(现在看起来很明显),但由于花了很长时间,我决定在这里添加一个问答。
最佳答案
关于ios - 如何计算 NSLayoutManager 中的字形数量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35063184/