OGeek|极客世界-中国程序员成长平台

标题: ios - 将 TextKit 与 Storyboard 一起使用 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-13 07:50
标题: ios - 将 TextKit 与 Storyboard 一起使用

我在带有 UINavigationController 的 Storyboard 中有一个 UITextView,如下所示:

enter image description here

它在顶部有一个自动布局约束,以防止最上面的文本隐藏在导航栏下方,并在左、右和底部有一个约束。

我需要使用自定义 NSTextStorage 子类,据我所知,这需要我手动实例化 UITextView 并将其插入到 View 中。我的问题是,如何用代码模拟这些约束?

这是我为初始化 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];

顺便说一句,如果有办法在 Storyboard 中使用带有 UITextView 的自定义 UITextStorage 子类,我可以解决这个问题,我是不是误以为这个了不可能吗?



Best Answer-推荐答案


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

关于ios - 将 TextKit 与 Storyboard 一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23672622/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://sqlite.in/) Powered by Discuz! X3.4