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