如何获得其中的字形总数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 答案。
我终于在文档中找到了它(现在看起来很明显),但由于花了很长时间,我决定在这里添加一个问答。