2016-01-28 58 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英寸

相關問題