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

挺方便啊

posted @ 2016-05-19 15:50  Emyin  阅读(279)  评论(0编辑  收藏  举报