现在的位置: 首页 > 综合 > 正文

让UIKit(UIView,UILabel等)直接显示网页内容

2017年12月08日 ⁄ 综合 ⁄ 共 486字 ⁄ 字号 评论关闭
NSString *html = @"<bold>Wow!</bold> Now <em>iOS</em> can create <h3>NSAttributedString</h3> from HTMLs!";
    NSDictionary *options = @{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType};
    
    NSAttributedString *attrString = [[NSAttributedString alloc] initWithData:[html dataUsingEncoding:NSUTF8StringEncoding]
                                                                      options:options documentAttributes:nil error:nil];

    CATextLayer *textLayer = [CATextLayer layer];
    textLayer.string = attrString;
    textLayer.frame = CGRectMake(10, 60, 300, 400);
    [self.view.layer addSublayer:textLayer];

抱歉!评论已关闭.