iOS Development Web Chapter--OC loading HTML code

Source: Internet
Author: User

HTML code

Figure 1

Style one: "<p> Test content information Error-free </p>" Style II: 

Figure 2

First, Scenario 1: Load onto UILabel (convert to Rich Text)

 1. Convert a string to a standard HTML string nsstring *str1 = [self HTMLENTITYDECODE:HTMLSTRING];//2. Converting an HTML string to AttributeString nsattributed String * Attributestr = [self attributedstringwithhtmlstring:str1];    3. Use the label to load the HTML string and add the label to the view Self.label.attributedText = Attributestr;    [Self.label Setframe:cgrectmake (100,100,200,300)]; [Self.view addsubview:self.label];//------HTML to string//convert similar characters such as &lt to "<" in HTML-(NSString *)    Htmlentitydecode: (NSString *) string{string = [string stringbyreplacingoccurrencesofstring:@ "" "withstring:@" \ ""];    string = [string stringbyreplacingoccurrencesofstring:@ "'" withstring:@ "'"];    string = [string stringbyreplacingoccurrencesofstring:@ "<" withstring:@ "<"];    string = [string stringbyreplacingoccurrencesofstring:@ ">" withstring:@ ">"]; string = [string stringbyreplacingoccurrencesofstring:@ "&" withstring:@ "&"]; e.g @ "<" goes to @ "<" not @ "<" return string; ------Convert HTML to Rich text//HTML charactersStrings converted to nsattributedstring Rich text string-(nsattributedstring *) attributedstringwithhtmlstring: (NSString *) htmlstring{ Nsdictionary *options = @{Nsdocumenttypedocumentattribute:nshtmltextdocumenttype, NSChara    Cterencodingdocumentattribute: @ (nsutf8stringencoding)};    NSData *data = [htmlstring datausingencoding:nsutf8stringencoding]; return [[Nsattributedstring alloc] initwithdata:data options:options documentattributes:nil error:nil];} The style 2 in Figure 2 can appear blank, can be removed by the HTML tag to process//remove the tag in the HTML string-(NSString *) filterhtml: (NSString *) html{Nsscanner * scanner = [NS    Scanner scannerwithstring:html];    NSString * Text = nil;        while ([scanner isatend]==no) {//Find the starting position of the label [Scanner scanuptostring:@ "<" Intostring:nil];        Find the end position of the label [Scanner scanuptostring:@ >] intostring:&text]; Replace character html = [html stringbyreplacingoccurrencesofstring:[nsstring stringwithformat:@ "%@>", text] withstring:@ ""]    ; } return HTML;}

Ii. Scenario 2: Loading onto UIWebView (replacing the characters in the HTML section)

    1. Convert the string to a standard HTML string, where the string is not a standard tag HTML string, and the string is converted to a standard HTML string so that the HTML string can be loaded)    nsstring *str1 = [self htmlentitydecode:htmlstring];//Invoke Scenario 1 method   //2.uiwebview Load HTML string    UIWebView * WebView = [[UIWebView alloc] Initwithframe:cgrectmake (, self.view.frame.size.width-20, ())];    [WebView loadhtmlstring:str1 Baseurl:nil];    [Self.view Addsubview:webview];

  

  

iOS Development Web Chapter--OC loading HTML code

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.