2016-01-28 55 views
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.

回答

1

使用numberOfGlyphs属性,如

layoutManager.numberOfGlyphs 

这表现在Text Layout编程指南文档here英寸

相关问题