ios - 如何计算 NSLayoutManager 中的字形数量

标签 ios swift glyph nslayoutmanager

如何获取 NSLayoutManager 中的字形总数?

我正在制作一个光标层以覆盖在自定义 TextView 上,如下所示:

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

这在文本布局编程指南文档中进行了演示 here .

关于ios - 如何计算 NSLayoutManager 中的字形数量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35063184/

相关文章:

ios - 启动使用 Simperium 的应用程序时 objectFromJSONString 崩溃

iphone - 使用后退按钮时 UISwitch 会切换到 OFF 状态

ios - swift 中的@property/@synthesize 等价物

java - 调整 iText 中的 TextMatrix

ios - 设置图像 :forState: does not work in iOS 3. 2

ios - 隐藏组件不占位置

ios - 显示在 TableView 部分标题顶部的编辑行操作

swift - 在 Swift 中从 TableViewController(和其他)外部调用函数/传递数据

html - 如何在不更改字体大小的情况下更改字形高度?

python - 在 Python 中测试一个字形是否是同一字体中另一个字形的反射(reflect)