1

如何获得其中的字形总数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 答案。

我终于在文档中找到了它(现在看起来很明显),但由于花了很长时间,我决定在这里添加一个问答。

4

1 回答 1

1

使用该numberOfGlyphs属性,如

layoutManager.numberOfGlyphs

这在此处的文本布局编程指南文档中进行了演示。

于 2016-01-28T13:44:35.673 回答