How to replace NSLayoutManager text storage

I am trying to create a table view cell that has a UITextView. To reuse this table view cell, I installed a new text store in the layout manager. But adding a layout manager to the new text store does not change the text store of the text view.

The layout manager, text container, text view are configured as follows:

_myLayoutManager = [[NSLayoutManager alloc] init]; _myTextContainer = [[NSTextContainer alloc] init]; [_myLayoutManager addTextContainer:_myTextContainer]; _myTextView = [[UITextView alloc] initWithFrame:self.bounds textContainer:_myTextContainer]; 

When I “customize” the table view cell, I add the layout manager to the new text store:

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

It 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, through ...

 [self.myTextView systemLayoutSizeFittingSize:...] 

... get the wrong size. Looking at the properties of the text view, I realized that it still indicates that the text was not stored properly.

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

This is really weird. Do I need to configure the entire stack again (layout manager, text container, text view)?

If so, I will need to start a new text view, as well as update the layout constraints, which will be quite expensive.

+5
source share

Source: https://habr.com/ru/post/1212136/


All Articles