ios boundingRectWithSize 方法使用
- 此方法是ios中计算
有属性的文字
所需要占有的空间矩形.
直接上Demo
代码:
UILabel *lbTemp =[[UILabel alloc] initWithFrame:CGRectMake(20, 20, 100, 700)];
lbTemp.backgroundColor = [UIColor brownColor];
lbTemp.lineBreakMode = NSLineBreakByCharWrapping;
lbTemp.numberOfLines = 0;
lbTemp.text = @"天天\n上课,还挂科,是在是蛋疼.....希望这个地球更加美丽漂亮,随便写些东西都不是容易的事情啊";
NSRange allRange = [lbTemp.text rangeOfString:lbTemp.text];
[self.view addSubview:lbTemp];
NSMutableParagraphStyle *tempParagraph = [[NSMutableParagraphStyle alloc] init];
tempParagraph.lineSpacing = 20;
tempParagraph.firstLineHeadIndent = 20.f;
NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc] initWithString:lbTemp.text];
[attrStr addAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:25],NSForegroundColorAttributeName:[UIColor redColor],NSParagraphStyleAttributeName:tempParagraph} range:allRange];
CGRect rect = [attrStr boundingRectWithSize:CGSizeMake(200, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading context:nil];
lbTemp.frame = CGRectMake(50, 50, rect.size.width, rect.size.height);
lbTemp.attributedText = attrStr;
Demo效果:
DEMO地址:https://github.com/jxjgssylsg/YLUtilityDemos
调用方法1:
方法1:boundingRectWithSize Demo 1.算出来了具有属性的文字所占有的空间 2.ParagraphStyle属性,例如字体行间距,首行缩进