美文网首页
Label多行显示,文字设置行高

Label多行显示,文字设置行高

作者: 放肆的洒脱 | 来源:发表于2018-04-07 16:56 被阅读37次

根据要求label多行显示,只需要设置numberOfLines即可,设置行高就需要用到富文本

UILabel *detailLabel=[[UILabel alloc]initWithFrame:CGRectMake(0, 100, 320, 100)];
 MessageDetailLabel.text=@"测试数据label多行显示并带有行高试数据label多行显示并带有行高试数据label多行显示并带有行高试数据label多行显示并带有行高试数据label多行显示并带有行高";
detailLabel.numberOfLines=2;
 NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:MessageDetailLabel.text];
  NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
  [paragraphStyle setLineSpacing:10];//调整行间距
  [attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [MessageDetailLabel.text length])];
detailLabel.attributedText = attributedString;
detailLabel.lineBreakMode = NSLineBreakByTruncatingTail;
detailLabel.textColor=[UIColor blackColor];
detailLabel.font=[UIFont systemFontOfSize:13];
 [self.view addSubview:MessageDetailLabel];

因为我们的项目要求最多显示两行,所以设置了numberOfLines=2,如果不需要设置最多显示的行数,主需要设置为0即可;lineBreakMode = NSLineBreakByTruncatingTail;这个是为了两行显示的时候,如果显示不完,末尾用...来表示

相关文章

网友评论

      本文标题:Label多行显示,文字设置行高

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