How to replace the text storage of an NSLayoutManager
Asked Answered
K

0

7

I'm trying to build a table view cell that has a UITextView. To reuse this table view cell, I set a new text storage to the layout manager. But adding the layout manager to the new text storage does not change the text storage of the text view.

The layout manager, text container, text view are set up like this:

_myLayoutManager = [[NSLayoutManager alloc] init];
_myTextContainer = [[NSTextContainer alloc] init];

[_myLayoutManager addTextContainer:_myTextContainer];

_myTextView = [[UITextView alloc] initWithFrame:self.bounds
                                  textContainer:_myTextContainer];

When I "configure" the table view cell I add the layout manager to a new text storage:

MyTextStorage *textStorage = ...
[textStorage addLayoutManager:self.myLayoutManager];

This works so far, that the new text will be displayed correctly. But when I reuse the cell (adding the layout manager to another text storage) and getting the size of the text view, via …

[self.myTextView systemLayoutSizeFittingSize:...]

… get the wrong size. After looking at the properties of the text view I figured out, that it still points to the wrong text storage.

self.myLayoutManager.textStorage -> <new text storage>
self.myTextView.textStorage -> <old text storage>

This feels really strange. Do I need to setup the whole stack again (layout manager, text container, text view)?

If yes, I would need to initiate a new text view and also update the layout constraints, which would be quite expensive.

Kowtko answered 28/1, 2015 at 20:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.