replace layout manager of uitextview

5.3k views Asked by At

NSTextContainer on Mac OS X has a method replaceLayoutManager: to replace the NSLayoutManager of NSTextView with a subclass of NSLayoutManager.

Unfortunately iOS doesn't have such a function. I tried a combination of these lines of code, but it keeps crashing.

THLayoutManager *layoutManager = [[THLayoutManager alloc] init];
    [layoutManager addTextContainer:[self textContainer]];

    //      [[self textStorage] removeLayoutManager:[self layoutManager]];
    //[[self textStorage] addLayoutManager:layoutManager];
    [[self textContainer] setLayoutManager:layoutManager];

What is the correct procedure to replace the NSLayoutManager of an UITextview?

2

There are 2 answers

3
Duncan Groenewald On

Have a look at the WWDC2013 Intro To Text Kit video and sample code where they show how to do it.

https://developer.apple.com/downloads/index.action?name=WWDC%202013 https://developer.apple.com/wwdc/videos/

Below is an extract from the code

-(void)viewDidLoad
{
    [super viewDidLoad];

    // our auto layout views use a design spec that calls for
    // 8 pts on each side except the bottom
    // since we scroll at the top here, only inset the sides

    CGRect newTextViewRect = CGRectInset(self.view.bounds, 8., 0.);

    self.textStorage = [[TKDInteractiveTextColoringTextStorage alloc] init];

    NSLayoutManager *layoutManager = [[NSLayoutManager alloc] init];

    NSTextContainer *container = [[NSTextContainer alloc] initWithSize:CGSizeMake(newTextViewRect.size.width, CGFLOAT_MAX)];
    container.widthTracksTextView = YES;
    [layoutManager addTextContainer:container];
    [_textStorage addLayoutManager:layoutManager];

    UITextView *newTextView = [[UITextView alloc] initWithFrame:newTextViewRect textContainer:container];
    newTextView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
    newTextView.scrollEnabled = YES;
    newTextView.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag;

    [self.view addSubview:newTextView];
    self.textView = newTextView;

    self.textStorage.tokens = @{ @"Alice" : @{ NSForegroundColorAttributeName : [UIColor redColor] },
                                 @"Rabbit" : @{ NSForegroundColorAttributeName : [UIColor orangeColor] },
                                 TKDDefaultTokenName : @{ NSForegroundColorAttributeName : [UIColor blackColor] } };
}
0
kjellie On

Since iOS9, NSTextContainer has the same method as macOS. So now you can replace the layout manager on your storyboard UITextView with your own subclass:

textView.textContainer.replaceLayoutManager(MyLayoutManager())