簡體   English   中英

如何在NSLayoutManager中計算字形的數量

[英]How to count the number of glyphs in NSLayoutManager

如何獲取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答案。

我終於在文檔中找到了它(現在看起來很明顯),但是由於花了很長時間,所以我決定在這里添加一個問答。

使用numberOfGlyphs屬性,如下所示

layoutManager.numberOfGlyphs

這在此處的《文本布局編程指南》文檔中得到了證明。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM