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);

你可能感兴趣的:(iOS开发UILabel设置不同的字体颜色设置行间距,获取大小)