UIKit控件直接显示网页文字内容

NSString *html = @"<bold>Hello</bold> Now<br> <em>iOS</em> can create <h2>NSAttributedString</h2> from <br>HTMLs!";
    NSDictionary *options = @{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType};
    
    NSAttributedString *attrString = [[NSAttributedString alloc] initWithData:[html dataUsingEncoding:NSUTF8StringEncoding]
                                                                      options:options documentAttributes:nil error:nil];
    CATextLayer *textLayer = [CATextLayer layer];
    textLayer.backgroundColor = [UIColor redColor].CGColor;
    textLayer.string = attrString;
    textLayer.frame = CGRectMake(10, 60, 300, 400);
    [self.view.layer addSublayer:textLayer];

挺方便啊

原文地址:https://www.cnblogs.com/yintingting/p/5508956.html