iOS开发UILabel设置不同的字体颜色设置行间距,获取大小

NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:@"你的文字内容"];  
  
[str addAttribute:NSForegroundColorAttributeName  
            value:[UIColor blueColor]  
            range:NSMakeRange(0,5)];  

  
UILabel *attrLabel = [[UILabel alloc] init];  
attrLabel.attributedText = str;  
attrLabel.numberOfLines = 0;  
NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:@"Using NSAttributed String"];
[str addAttribute:NSForegroundColorAttributeName value:[UIColor blueColor] range:NSMakeRange(0,5)];
[str addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(6,12)];
[str addAttribute:NSForegroundColorAttributeName value:[UIColor greenColor] range:NSMakeRange(19,6)];
[str addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Arial-BoldItalicMT" size:30.0] range:NSMakeRange(0, 5)];
[str addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"HelveticaNeue-Bold" size:30.0] range:NSMakeRange(6, 12)];
[str addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Courier-BoldOblique" size:30.0] range:NSMakeRange(19, 6)];
attrLabel.attributedText = str;
    NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:model];
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    [paragraphStyle setLineSpacing:10];//调整行间距
    [str addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [model length])];
    [str addAttribute:NSForegroundColorAttributeName value:[MyParentClass colorWithHexString:@"556d93"] range:NSMakeRange(2,5)];
    _context.attributedText=str;
    
    
    //消息内容frame
    
    //先给定一个宽度,高度无限大
    
    CGFloat CommenntWidth = WIDTH;
    
    CGSize maxSize = CGSizeMake(CommenntWidth, MAXFLOAT);
    
    //设定attributedString的字体及大小,一定要设置这个,否则计算出来的height是非常不准确的
    
    [str addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:15] range:NSMakeRange(0, [str length])];
    
    //计算attributedString的rect
    
    CGRect conmentSize = [str boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin context:nil];
    _context.sd_layout.heightIs(conmentSize.size.height);
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容