美文网首页iOS 知识点
iOS UILabel显示html内容

iOS UILabel显示html内容

作者: flionel | 来源:发表于2017-12-28 19:09 被阅读24次
    // 将html内容转化为attributedString
    NSString * htmlString = @"<font color='#4085de'> worldcpp </font> <font color='#333333'>来啦 ~</font> 来啦🎉";  
    NSDictionary *options = @{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType };
    NSMutableAttributedString * attributeString = [[NSMutableAttributedString alloc] initWithData:[htmlString 
    dataUsingEncoding:NSUnicodeStringEncoding] 
    options:options 
    documentAttributes:nil error:nil];
    // 富文本字体、颜色等设置
    NSDictionary *attributes = @{ NSFontAttributeName: [UIFont systemFontOfSize:16] }
    [attributeString addAttributes: attributes, range: NSMageRange(location: 0, length: attributeString.length)];
    
    self.userComeLabel.attributedText = attributeString;
    // 计算富文本的size
    CGSize textSize = [attributeString boundingRectWithSize:CGSizeMake(CGFLOAT_MAX, 26)
     options:NSStringDrawingUsesFontLeading 
    context:nil].size;
    CGFloat userComeLabelWidth = textSize.width;
    

    相关文章

      网友评论

        本文标题:iOS UILabel显示html内容

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