Invalid glyph index when setting viewController layoutManager for subclass NSTextStorage

My goal is to use TextKit to italicize, set the size of the text, etc. certain words.

To get started, I'm just trying to highlight a character in a text string. As a newbie to TextKit (and truly for programming in general), I follow the syntax highlighting topic for obc.io issue # 5.

When using the NSLayoutManager built into the UITextView I created, my text appears on the screen without any exceptions. When I set UITextView as the layout manager of my subclass of NSTextStorage in my view controller (below), I get errors for exception for invalid glyph indices.

_textStorage = [BBRSyntaxHighlightTextStorage new]; [_textStorage addLayoutManager: self.readerTextView.layoutManager]; 

Console output below:

 _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 

I read Apple Programming Guide many times and I think I understand how the text system is installed, but I have no idea why my glyph count will exceed the number of glyphs ...

I created a gist for my subclass of viewController and NSTextStorage, here and here , respectively.

+7
ios objective-c textkit nstextstorage nslayoutmanager
source share
1 answer

It looks like your layout manager has not been correctly removed from the original NSTextStorage object. Call [self.readerTextView.textStorage removeLayoutManager:self.readerTextView.layoutManager] before adding the layout manager to a subclass of the user text store.

In general, I found that TextKit does not play well when mixing nib / xib / storyboards created by UITextViews with custom subclasses of TextKit objects. You can get around some of this by overriding - (id)awakeAfterUsingCoder:(NSCoder *)aDecoder in a UITextView and creating a text system using the classes you want, but I usually recommend sticking to the embedded text system in the code.

+9
source share

All Articles