![](/img/trans.png)
[英]NSLayoutManager: How to fill background colors where there are renderable glyphs only
[英]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答案。
我終於在文檔中找到了它(現在看起來很明顯),但是由於花了很長時間,所以我決定在這里添加一個問答。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.