iOS:使用 sizeWithFont:constrainedToSize:lineBreakMode 的 UILabel 动态高度:不起作用
我试图给我的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将
maximumLabelSize
设置为的行更改为change the line where you set
maximumLabelSize
to在您现在的代码中,无论方向如何,您都将获得相同的宽度和高度,因为您始终将宽度 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 thesizeWithFont
will also change dynamically.