我试图随着文本宽度的变化动态更改uilabel高度,但当我尝试使用这行"white wallington hall"时出现了问题。基本上,它计算的宽度是uilabel中的两行,但当它显示为两行时,它看起来是这样的:
"white
wallington h..."
样本代码:
UILabel* lbl_Property = [[UILabel alloc]init];
[lbl_Property setFont:[UIFont fontWithName:@"Arial" size:10]];
[lbl_Property setNumberOfLines:0];
[lbl_Property setText:[arr_SubDetail objectAtIndex:x]];
UIFont* font = [UIFont fontWithName:@"Arial" size:10];
CGSize stringSize = [lbl_Property.text sizeWithFont:font];
CGFloat width = stringSize.width;
[lbl_Property setFrame:CGRectMake(lbl_Property.frame.origin.x, lbl_Property.frame.origin.y, lbl_Property.frame.size.width,5+lbl_Property.frame.size.height*(ceilf(width/110)))];
事实上,在"白色"之后有一个空格,"wallington"不在那一行,所以它在新的一行上,但它没有显示完整的文本
我该如何正确显示?
试试……
//Calculate the expected size based on the font and linebreak mode of your label
CGSize maximumLabelSize = CGSizeMake(296,9999);
CGSize expectedLabelSize = [yourString sizeWithFont:yourLabel.font
constrainedToSize:maximumLabelSize
lineBreakMode:yourLabel.lineBreakMode];
//adjust the label the the new height.
CGRect newFrame = yourLabel.frame;
newFrame.size.height = expectedLabelSize.height;
yourLabel.frame = newFrame;
int h=10;
UILabel *lbl_headitem = [[UILabel alloc]initWithFrame:CGRectMake(3,h, 690, size_txt_overview1.height)];// See the 'h' value
lbl_headitem.text = [headItemArray objectAtIndex:k];
[lbl_headitem setTextAlignment:UITextAlignmentLeft];
[lbl_headitem setBackgroundColor:[UIColor clearColor]];
[lbl_headitem setTag:k];
lbl_headitem.numberOfLines=0;
[lbl_headitem setTextColor:[UIColor redColor]];
[lbl_headitem setFont:[UIFont fontWithName:@"Helvetica" size:18]];
[scrollAttributes addSubview:lbl_headitem];
h = h + size_txt_overview1.height;
确保您的label.numberOfLines设置为0
希望以下代码可以在中工作
CGSize size = [str sizeWithFont:lbl.font constrainedToSize:CGSizeMake(204.0, 10000.0) lineBreakMode:lbl.lineBreakMode];
这里的宽度或高度必须固定。。
func makeSizeSender(name:UILabel){
let attributes = [NSFontAttributeName : name.font]
let rect = name.text!.boundingRectWithSize(CGSizeMake(frame.size.width, CGFloat.max), options: NSStringDrawingOptions.UsesLineFragmentOrigin, attributes: attributes, context: nil)
name.frame = CGRectMake(contentView.frame.origin.x+30, 0, 200, rect.height+30);
self.addSubview(name)
}