根据文本长度,固定label的宽度和字体大小,画label的宽高。
效果图:
代码如下:
//根据文本长度计算label的宽高
NSString * str=@"天将降大任于斯人也,必先苦其心志,劳其筋骨,饿其体肤,空乏其身,行夫乱其所为。天将降大任于斯人也,必先苦其心志,劳其筋骨,饿其体肤,空乏其身,行夫乱其所为。天将降大任于斯人也,必先苦其心志,劳其筋骨,饿其体肤,空乏其身,行夫乱其所为。天将降大任于斯人也,必先苦其心志,劳其筋骨,饿其体肤,空乏其身,行夫乱其所为。";
CGRect tempRect = [str boundingRectWithSize:CGSizeMake([UIScreenmainScreen].bounds.size.width-40,2000)options:NSStringDrawingUsesLineFragmentOriginattributes:@{NSFontAttributeName:[UIFontsystemFontOfSize:17]}context:nil];
UILabel * label = [[UILabelalloc]initWithFrame:CGRectMake(20,100, tempRect.size.width, tempRect.size.height)];
label.text = str;
label.numberOfLines =0;
[self.viewaddSubview:label];
UIImageView * image = [[UIImageViewalloc]initWithFrame:CGRectMake(20, label.frame.size.height+label.frame.origin.y+10,200,200)];
image.backgroundColor = [UIColorredColor];
[self.viewaddSubview:image];