replace layout manager of uitextview
Asked Answered
T

2

4

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?

Teheran answered 3/1, 2014 at 17:54 Comment(0)
U
6

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())
Unexperienced answered 13/4, 2020 at 2:25 Comment(0)
M
3

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] } };
}
Maximilianus answered 4/1, 2014 at 2:26 Comment(3)
A new UITextView is created here. Can't we do this from a storyboard?Dacy
@nr5 you can do textView.textContainer.replaceLayoutManager(MyLayoutManager())Byars
Was not able to made this method work with UITextView instantiated from Storyboard.Xenogenesis

© 2022 - 2024 — McMap. All rights reserved.