调整大小后从 UILabel 获取 numberOfLines

发布于 2024-10-30 02:26:19 字数 851 浏览 2 评论 0原文

我这样做是为了制作我的 UIlabel,将 numberOfLines 设置为 0,以便它没有行限制:

    UILabel *nmLbl = [[UILabel alloc] initWithFrame:CGRectZero];
    [nmLbl setFont:[UIFont boldSystemFontOfSize:16.0f]];
    [nmLbl setNumberOfLines:0];
    [self addSubview:nmLbl];
    [nmLbl release];

稍后,当我知道哪个字符串进入标签时,我像这样调整它的大小:

    nameSize = [[self name] sizeWithFont:[UIFont boldSystemFontOfSize:16.0f] constrainedToSize:maxNameSize lineBreakMode:UILineBreakModeWordWrap];
    [self.nameLabel setFrame:CGRectMake(0.0f, 0.0f, nameSize.width, nameSize.height)];
    [self.nameLabel sizeToFit];

现在,对于我的特定用途,我需要知道这最终需要多少行。

如果我访问 UILabelnumberOfLines 属性,它将始终返回 0。

有没有办法让我直接访问 UILabel 最终没有多少行 必须再次通过 label.size.height / fontHeight 来计算它吗?

先感谢您。

I do this to make my UIlabel, setting the numberOfLines to 0 so that it has no line-limit:

    UILabel *nmLbl = [[UILabel alloc] initWithFrame:CGRectZero];
    [nmLbl setFont:[UIFont boldSystemFontOfSize:16.0f]];
    [nmLbl setNumberOfLines:0];
    [self addSubview:nmLbl];
    [nmLbl release];

Later on, when I know which string goes into the label, I size it like this:

    nameSize = [[self name] sizeWithFont:[UIFont boldSystemFontOfSize:16.0f] constrainedToSize:maxNameSize lineBreakMode:UILineBreakModeWordWrap];
    [self.nameLabel setFrame:CGRectMake(0.0f, 0.0f, nameSize.width, nameSize.height)];
    [self.nameLabel sizeToFit];

Now, for my particular use, I need to know how many lines this ends up taking.

If I access the numberOfLines property of the UILabel it will always return 0.

Is there a way for me to directly access how many lines the UILabel ended up being without
having to calculate it, again, by going label.size.height / fontHeight?

Thank you in advance.

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

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

发布评论

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

评论(1

っ〆星空下的拥抱 2024-11-06 02:26:19

No. numberOfLines 是一个配置设置,并不反映当前格式。

No. numberOfLines is a configuration setting, not a reflection of the current formatting.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文