当 UILabel 中的文本发生变化时如何重新绘制 UIView?

发布于 2025-01-08 21:00:22 字数 204 浏览 7 评论 0原文

我有一个 UIView 的子类,其中有一个 UILabel 作为子视图添加到视图中。在 layoutSubviews 方法中,计算 UILabel 的位置和高度。我的问题是,当 UILabel 的文本发生变化时,应该重新计算这个高度。文本从 UIViewController 插入到标签中,因此视图不知道这种情况何时发生。

I've a subclass of UIView with a UILabel with is added as subview to the view. In the layoutSubviews method the position and height of the UILabel is calculated. My problem is, that this height should be recalculated, when the text of the UILabel changes. The text is inserted into the label from the UIViewController, so the view does not know when this happen.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

三月梨花 2025-01-15 21:00:22

您可以子类化 UILabel 来覆盖 setText:,如下所示:

@implementation MyLabel

- (void)setText:(NSString *)text {
    [super setText:text];
    [self.superview setNeedsLayout];
}

You can subclass UILabel to override setText: like this:

@implementation MyLabel

- (void)setText:(NSString *)text {
    [super setText:text];
    [self.superview setNeedsLayout];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文