iOS:使用 sizeWithFont:constrainedToSize:lineBreakMode 的 UILabel 动态高度:不起作用

发布于 2024-10-29 09:22:57 字数 1414 浏览 4 评论 0原文

我试图给我的 UILabel 动态高度,以便我的其他标签的布局在横向和纵向上看起来都是正确的。

在纵向中,我的文本会换行到第二行,而在横向中则不会。因此,当使用 sizeWithFont:constrainedToSize:lineBreakMode: 时,双向旋转时我得到相同的高度,而我假设当文本为 2 行时它会是一个更大的数字。

当 UILabel 有两行或更多文本(纵向)时,如何获取它的高度,并在横向时获取一行的新高度?

我想我不明白如何让动态高度发挥作用......

UILabel *itemTitle = [[UILabel alloc] initWithFrame:CGRectMake(10.0f, top, screen.size.width - 20, 200.0f)];
itemTitle.text = self.newsAsset.title;
itemTitle.adjustsFontSizeToFitWidth = NO;
itemTitle.autoresizingMask = UIViewAutoresizingFlexibleWidth;
itemTitle.font = [UIFont boldSystemFontOfSize:18.0];
itemTitle.textColor = [UIColor blackColor];
itemTitle.shadowColor = [UIColor whiteColor];
itemTitle.shadowOffset = CGSizeMake(0, 1);
itemTitle.backgroundColor = [UIColor blueColor];
itemTitle.lineBreakMode = UILineBreakModeWordWrap;
itemTitle.numberOfLines = 0;
[itemTitle sizeToFit];

// Set the height
CGSize maximumLabelSize = CGSizeMake(300,9999);
CGSize titleSize = [itemTitle.text sizeWithFont:itemTitle.font constrainedToSize:maximumLabelSize lineBreakMode:itemTitle.lineBreakMode];

NSLog(@"Height: %.f  Width: %.f", titleSize.height, titleSize.width);

//Adjust the label the the new height
CGRect newFrame = itemTitle.frame;
newFrame.size.height = titleSize.height;
itemTitle.frame = newFrame;

// Add them!
[headerView addSubview:itemTitle];
[itemTitle release];

top += titleSize.height;

I am trying to give my UILabel dynamic height so that my layout of other labels looks correct in both landscape and portrait.

In portrait, my text wraps to the second line, in landscape it does not. So, when using sizeWithFont:constrainedToSize:lineBreakMode: I get the same height when rotating both ways, when I had assumed it would be a larger number when the text was 2 lines.

How can I get the height of my UILabel when it has two lines of text or more (portrait) and get the new height which is one line, when in landscape?

I guess I am not understanding how to get dynamic height working...

UILabel *itemTitle = [[UILabel alloc] initWithFrame:CGRectMake(10.0f, top, screen.size.width - 20, 200.0f)];
itemTitle.text = self.newsAsset.title;
itemTitle.adjustsFontSizeToFitWidth = NO;
itemTitle.autoresizingMask = UIViewAutoresizingFlexibleWidth;
itemTitle.font = [UIFont boldSystemFontOfSize:18.0];
itemTitle.textColor = [UIColor blackColor];
itemTitle.shadowColor = [UIColor whiteColor];
itemTitle.shadowOffset = CGSizeMake(0, 1);
itemTitle.backgroundColor = [UIColor blueColor];
itemTitle.lineBreakMode = UILineBreakModeWordWrap;
itemTitle.numberOfLines = 0;
[itemTitle sizeToFit];

// Set the height
CGSize maximumLabelSize = CGSizeMake(300,9999);
CGSize titleSize = [itemTitle.text sizeWithFont:itemTitle.font constrainedToSize:maximumLabelSize lineBreakMode:itemTitle.lineBreakMode];

NSLog(@"Height: %.f  Width: %.f", titleSize.height, titleSize.width);

//Adjust the label the the new height
CGRect newFrame = itemTitle.frame;
newFrame.size.height = titleSize.height;
itemTitle.frame = newFrame;

// Add them!
[headerView addSubview:itemTitle];
[itemTitle release];

top += titleSize.height;

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

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

发布评论

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

评论(2

那伤。 2024-11-05 09:22:57

maximumLabelSize 设置为的行更改为

CGSize maximumLabelSize = CGSizeMake(headerView.bounds.size.width, CGFLOAT_MAX);

change the line where you set maximumLabelSize to

CGSize maximumLabelSize = CGSizeMake(headerView.bounds.size.width, CGFLOAT_MAX);
梦归所梦 2024-11-05 09:22:57

在您现在的代码中,无论方向如何,您都将获得相同的宽度和高度,因为您始终将宽度 300 传递给 sizeWithFont 方法。如果将其设为动态,也许 sizeWithFont 的结果也会动态变化。

In your code as it is now, in either orientation you will get the same width and height, since you always pass a width of 300 to the sizeWithFont method. If you make it dynamic, maybe the result of the sizeWithFont will also change dynamically.

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