2

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?

4

1 回答 1

4

为了回答我自己的问题,这里的解决方案是将 UITextView 子类化,在子类中实现我的自定义行为,然后在情节提要中设置 UITextView 以使用子类。这让我可以维护故事板中的初始约束集。

于 2014-05-17T08:36:15.763 回答