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