美文网首页
准确计算UIlabel的宽高

准确计算UIlabel的宽高

作者: 哥只是个菜鸟 | 来源:发表于2020-05-15 16:24 被阅读0次
  • 准确计算UIlabel的宽高
height = [self.label.text sizeThatFits:CGSizeMake(SCREEN_WIDTH - 30, MAXFLOAT) ].height;
  • 设置UIlabel的行间距和部分颜色

NSMutableAttributedString* text =[[NSMutableAttributedString alloc]initWithString:string];

NSMutableParagraphStyle * paragraphStyle =[[NSMutableParagraphStyle alloc]init];

//设置行距

[paragraphStyle setLineSpacing:15.0f];

[text addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, string.length)];

//设置一定Range区间文字颜色

[text addAttribute:NSForegroundColorAttributeName value:[UIColor greenColor] range:NSMakeRange(0, 30)];

//设置一定Range区间文字下划线

[text addAttribute:NSUnderlineStyleAttributeName value:@(NSUnderlineStyleSingle) range:NSMakeRange(0, 30)];

label.attributedText = text;

[self.view addSubview:label];

[label sizeToFit];

相关文章

网友评论

      本文标题:准确计算UIlabel的宽高

      本文链接:https://www.haomeiwen.com/subject/fcocohtx.html