1
如何获取NSLayoutManager
中的字形总数?如何统计NSLayoutManager中的字形数
我是在光标层覆盖在这样一个自定义文本视图:
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答案。
我终于找到了它埋在文档中(现在看起来很明显),但由于花了很长时间,我决定在这里添加一个Q & A.