开发者

Core Text to render HTML content

开发者 https://www.devze.com 2023-02-10 22:39 出处:网络
I was wondering if is there a way to render html content using core text, because the common UIWebView is slow as hell to load up.

I was wondering if is there a way to render html content using core text, because the common UIWebView is slow as hell to load up.

I've got very basic needs:

- reading p tag开发者_StackOverflows

- reading span tags

- using colors

- using fonts and sizes

that's all.

thanks you so much!

k


ok I solved this in a bit tricky way.
I implemented a HTMLParser to extract the characters that I needed to be formatted in a different way. than I proceeded to use the string in Core-Text.

my references were

[HTMLParser] https://github.com/zootreeves/Objective-C-HMTL-Parser
remember that this one has a problem by parsing two tags one after another
(e.g. < p >< span >. . . < /span >< /p >)

[kickstart for CoreText] https://github.com/jonasschnelli/I7CoreTextExample


See this open-source project: https://github.com/Cocoanetics/DTCoreText

It won't help you render an attributed string with Core Text, but it will help you convert HTML to an NSAttributedString.


This question has already been answered better here:

https://stackoverflow.com/a/18886718/187258

No need to use DTCoreText if you're targeting iOS 7 and up (which you should be doing, by now).

Here's a reprint of the code in the answer (important flag: NSHTMLTextDocumentType):

[[NSAttributedString alloc] initWithData:[htmlString dataUsingEncoding:NSUTF8StringEncoding] 
                                 options:@{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType,
                                           NSCharacterEncodingDocumentAttribute: @(NSUTF8StringEncoding)} 
                      documentAttributes:nil 
                                   error:nil];
0

精彩评论

暂无评论...
验证码 换一张
取 消