美文网首页
iOS 富文本方法

iOS 富文本方法

作者: 小辉辉___ | 来源:发表于2016-12-24 15:24 被阅读13次

- (CGFloat)calculateHeightOfString:(NSString*)string
{
    CGRect firTextRect = [string boundingRectWithSize:CGSizeMake(ScreenWidth-20, 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:12]} context:nil];
    return firTextRect.size.height;
}

- (NSMutableAttributedString *)getTextOfLabelWithFirUserName:(NSString *)firUserName secUserName:(NSString*)secUserName content:(NSString*)content
{
    NSString *string = nil;
    if (firUserName.length > 0) {
        if (secUserName.length > 0) {
            string = [NSString stringWithFormat:@"%@回复%@:%@",firUserName,secUserName,content];
        }
        else
        {
            secUserName = @"";
            string = [NSString stringWithFormat:@"%@:%@",firUserName,content];
        }
    }
    else
    {
        firUserName = @"";
        string = [NSString stringWithFormat:@"回复%@:%@",secUserName,content];
    }
    
    NSMutableAttributedString *temString = [[NSMutableAttributedString alloc]initWithString:string];
    [temString addAttribute:NSForegroundColorAttributeName
                      value:[UIColor colorWithHexString:@"0084ad"]
                      range:NSMakeRange(0, firUserName.length)];
    [temString addAttribute:NSForegroundColorAttributeName
                      value:[UIColor colorWithHexString:@"0084ad"]
                      range:NSMakeRange(firUserName.length + 2, secUserName.length)];
    return temString;
}

相关文章

网友评论

      本文标题:iOS 富文本方法

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