我的目标是使用 TextKit 斜体,设置某些单词的文本大小等。
首先,我只是想突出显示文本字符串中的一个字符。作为 TextKit 的新手(并且对于一般的编程来说如实说),我正在关注 obc.io 问题 #5 的语法高亮主题。
当使用我创建的 UITextView 内置的 NSLayoutManager 时,我的文本出现在屏幕上,没有抛出异常。当我在视图控制器(如下)中将 UITextView 设置为 NSTextStorage 子类的布局管理器时,我收到无效字形索引异常的错误。
_textStorage = [BBRSyntaxHighlightTextStorage new];
[_textStorage addLayoutManager: self.readerTextView.layoutManager];
控制台输出如下:
_NSLayoutTreeLineFragmentRectForGlyphAtIndex invalid glyph index 528
2013-12-01 15:23:24.949 BibleReader[6077:70b] !!! _NSGlyphTreeInvalidateGlyphsForCharacterRange invalid char range 1
2013-12-01 15:23:24.956 BibleReader[6077:70b] !!! _NSLayoutTreeLineFragmentRectForGlyphAtIndex invalid glyph index 528
2013-12-01 15:23:24.957 BibleReader[6077:70b] !!! _NSGlyphTreeInvalidateGlyphsForCharacterRange invalid char range 1
2013-12-01 15:23:24.957 BibleReader[6077:70b] !!! _NSGlyphTreeInvalidateGlyphsForCharacterRange character count mismatch
2013-12-01 15:23:24.958 BibleReader[6077:70b] !!! _NSLayoutTreeLineFragmentRectForGlyphAtIndex invalid glyph index 4040
2013-12-01 15:23:24.959 BibleReader[6077:70b] !!! _NSGlyphTreeInvalidateGlyphsForCharacterRange invalid char range 1
我多次阅读 Apple 的 Text Programming Guide 并认为我了解文本系统是如何建立的,但不知道为什么我的字形数会超过字形数......