我想让我的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;
constrainedToSize:
参数传递了什么?能展示一些代码吗? - mvds