iOS 另一種富文本轉化方式 html轉為富文本


直接用html轉化為iOS的富文本。

代碼如下:

-(void)demo2

{

    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(50, 100, 300, 300)];

    label.numberOfLine = 0;

    [self.view addSubview:label];

    

    NSString *content = @"\<p style=\"font-family:verdana;color:red\">    This text <a style=\"font-family:times;color:green;font-size:30px\">This text is in Times and green</a>is in Verdana and red</p><p style=\"font-size:50px\">This text is 50 pixels high</p>";

    

    NSData *data = [content dataUsingEncoding:NSUnicodeStringEncoding];

    NSDictionary *options = @{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType};

    NSAttributedString *html = [[NSAttributedString alloc]initWithData:data

                                                               options:options

                                                    documentAttributes:nil

                                                                 error:nil];

 

    label.attributedText = html;

}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM