gpt4 book ai didi

ios - 将 TextKit 与 Storyboard一起使用

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:43:09 24 4
gpt4 key购买 nike

我在 Storyboard中有一个 UITextView 和一个 UINavigationController,它看起来像这样:

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 子类,我就可以解决这个问题,我是否错误地认为这个不可能吗?

最佳答案

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

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

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com