Question

I have a UITextView in a storyboard with a UINavigationController, which looks like this:

enter image description here

It has an autolayout constraint at the top to prevent the uppermost text from being hidden beneath the navigation bar and a constraint to the left, right and bottom.

I need to make use of a custom NSTextStorage subclass, which I understand requires me to instantiate the UITextView manually and insert it into the view. My question is, how do I mimic these constraints with code?

Here is the code I've written to initialize the UITextView:

_textStorage = [[MyCustomTextStorage alloc] initWithString:@"some string"];

_layoutManager = [NSLayoutManager new];
[_textStorage addLayoutManager: _layoutManager];

_textContainer = [NSTextContainer new];
[_layoutManager addTextContainer: _textContainer];

_textView = [[UITextView alloc] initWithFrame:self.view.bounds textContainer:_textContainer];

[self.view addSubview:_textView];

As an aside, I could get around this if there were a way to use a custom UITextStorage subclass with a UITextView in a storyboard, am I mistaken in thinking this is impossible?

Was it helpful?

Solution

To answer my own question, the solution here was to subclass UITextView, implement my custom behaviour in the subclass and then set the UITextView in the storyboard to use the subclass. This lets me maintain the initial set of constraints from the storyboard.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top