7

我的目标是使用 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 并认为我了解文本系统是如何建立的,但不知道为什么我的字形数会超过字形数......

我为我的 viewController 和 NSTextStorage 子类创建了要点,分别在这里这里

4

1 回答 1

9

看起来您的布局管理器尚未从其原始NSTextStorage对象中正确删除。[self.readerTextView.textStorage removeLayoutManager:self.readerTextView.layoutManager]在将布局管理器添加到自定义文本存储子类之前调用​​。

UITextView一般来说,当将 nib/xib/storyboard created与 TextKit 对象的自定义子类混合时,我发现 TextKit 不能很好地发挥作用。您可以通过使用所需的类覆盖和构建文本系统来解决其中的一些问题- (id)awakeAfterUsingCoder:(NSCoder *)aDecoderUITextView但通常我建议坚持使用代码内自定义文本系统创建。

于 2013-12-11T04:39:20.077 回答