In iOS, OC loads the HTML string

Recently, the project encountered a HTML string, as follows:

In iOS, OC loads the HTML string
returns the style of the string back of the background

In iOS, there are usually two ways to load HTML strings

  • Loads the HTML string by loading rich text from the UILabel
  • Load the HTML string through WebView
- (void) viewDidLoad {[super viewDidLoad]; 1.UILabel load HTML string NSString * STR1 = @ & lt; div&; gt; Google (Chinese Name: Google) is an American multinational technology enterprise. &: lt; /div& gt; & lt; div&; gt; Google was created by Larry Paige and Sergei Blum who worked at Stanford University for science and engineering at that time; therefore, two people were also called "Google Guys"". & lt; /div& gt; & lt; Google; div& gt; in September 4, 1998, Google was founded in the form of Private Companies, designed and managed an Internet search engine". & lt; /div& gt; NSString * STR2 = @ "& lt; p& gt; & lt; br& gt; & lt; /p& gt; NSString * str3 = @" & lt; p& gt; qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq& lt; /p& gt; //1. string into standard HTML string STR1 = [self htmlEntityDecode:str1]; //2. HTML will convert a string into a attributeString * attributeStr = NSAttributedString [self attributedStringWithHTMLString:str1]; //3. HTML using label loading string self.label.attributedText = attributeStr; 2.UIWebView = UIWebView * webView loading HTML string [[UIWebView alloc]initWithFrame:CGRectMake (20, 300, self.view.frame.size.width - 40, 400)]; [webView loadHTMLString:str1 baseURL:nil]; [self.view addSubview:webView]; Self.webView = webView;} / / & lt and other similar characters into the HTML in the "<" - (NSString) htmlEntityDecode: (NSString * string) {string = [string stringByReplacingOccurrencesOfString:@ & quot; "withString:@" / "string = [string"]; stringByReplacingOccurrencesOfString:@ & apos; withString:@ "'"]; string = & [string stringByReplacingOccurrencesOfString:@ lt; "withString:@" < "]; string = [string; & gt; stringByReplacingOccurrencesOfString:@" withString:@ ">"]; string = [string; stringByReplacingOccurrencesOfString:@ & amp; & "withString:@" Do this last "]; / / so that e.g. & amp; @" lt "; goes to & lt; @ not @ <"; "return string;} / / HTML string into NSAttributedString Rich text string - (NSAttributedString *) attributedStringWithHTMLString: (NSString * htmlString) {NSDictionary *options = @{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType, NSCharacterEncodingDocumentAttribute: @ (NSUTF8StringEncoding)}; NSData *data = [htmlString dataUsingEncoding:NSUTF8StringEncoding]; return [[NSAttributedString alloc] initWithData:data options:options documentAttributes:nil error:nil];} / / remove the HTML string label (NSString *) filterHTML: (NSString * HTML) {NSScanner scanner = [NSScanner * NSString * scannerWithString:html]; text = nil; while ([scanner isAtEnd]==NO) {/ / find the starting position of [scanner scanUpToString:@ tags" < intoString:nil]; / / find the tag end position of the [scanner scanUpToString:@ ">"; intoString:& text]; / / HTML = [html stringByReplacingOccurrencesOfString:[NSString stringWithFormat:@ replace%@> ";" text], withString:@ "];} / / NSString * regEx = @" (< [^> >]* "); / / HTML = [html stringByReplacingOccurrencesOfString:regEx" withString:@ "]; return HTML;}

Be careful

  • The HTML string string is not a standard tag, so we should first call – (NSString) htmlEntityDecode: (NSString) string method will be converted into a standard HTML string string HTML string, so that it can be loaded
  • The second example of a string is empty, when loading with Label only two lines of blank data, this can be called – (NSString) filterHTML: (NSString) HTML method, remove labels, converting HTML string to string style commonly used