开发者

How to handle unknown UILabel height and its effect on labels underneath in Interface Builder?

开发者 https://www.devze.com 2023-01-30 19:18 出处:网络
I have two UILabels in a XIB file, one on top of the other, the contents of which are loaded from a JSON source file on the net.

I have two UILabels in a XIB file, one on top of the other, the contents of which are loaded from a JSON source file on the net.

Given that I'm unsure of the number of lines the labels will take up, and hence the height, how best should I position these relative to each other.

I know in some Java GUI frameworks, one can use various container elements, and in HTML this flow of layout would be the default, but I can't find anything that would seem to do the trick.

Can I do this in Interface Builder, or does this have to be done programmatically?

Thanks for your help..

Edit:

I now have the answer, al开发者_StackOverflow中文版though it may not be perfect. I have two labels, titleLabel above descLabel. This is how I achieved it:

titleLabel.text = [data objectForKey:@"title"];
descLabel.text  = [data objectForKey:@"description"];

CGSize s;
s.width = descLabel.frame.size.width;
s.height = 10000;



titleLabel.frame = CGRectMake(titleLabel.frame.origin.x, 
                             titleLabel.frame.origin.y,
                             titleLabel.frame.size.width,
                             [[data objectForKey:@"title"] sizeWithFont:titleLabel.font constrainedToSize: s lineBreakMode:titleLabel.lineBreakMode].height
                             );


descLabel.frame = CGRectMake(descLabel.frame.origin.x, 
                             titleLabel.frame.origin.y + [[data objectForKey:@"title"] sizeWithFont:titleLabel.font constrainedToSize: s lineBreakMode:titleLabel.lineBreakMode].height + 10,
                             descLabel.frame.size.width,
                             [[data objectForKey:@"description"] sizeWithFont:descLabel.font constrainedToSize: s lineBreakMode:descLabel.lineBreakMode].height
                             );

weffew


You’ll have to do this programatically. The NSString method -sizeWithFont:constrainedToSize:lineBreakMode: (which is actually in a UIKit category) will be of particular use for this.

0

精彩评论

暂无评论...
验证码 换一张
取 消