IOS analysis of HTMl tags and the development of some of the pit

At the beginning of

Look at the updated date for a long time did not write a book, and there are often small partners in the following comments, see what they write something is still a little bit to encourage themselves to stick to it, ha ha. Today is to write about how iOS parsing HTML tags, we used the background return data is usually JSON format but sometimes if we receive a HTMl tag we how to deal with him, just to say today.

text

The first two days to get the background data, get such a return information

"Congratulations on your getting < font color='red'> $8.1 < /font> cash reward"

It would have been simple to return the data to the label to show things suddenly become interesting, the background is to change the contents of the product back to the time when it is easy to deal with, this can be a very strong reason.

IOS analysis of HTMl tags and the development of some of the pit
is very strong

but as the development of how we deal with it, first of all, we look at the processing of Android, Android to deal with this is still relatively rude people have a systematic approach, as follows:

IOS analysis of HTMl tags and the development of some of the pit
Android html.png

iOS then end how to deal with it, we can use WebView, can also use the attributedString property of Label, can also split the string directly simply, and then remove the HTML tags, then, listen to let people disappointed, but after all is a method that can solve the problem of the method is a good method, our main code put the rough method (the other methods in the comments added, ha ha), following the removal of HTML tags:

Returns a string in the background / / filter label (NSString *) flattenHTML: (NSString * HTML) {NSScanner *theScanner NSString; *text = nil; theScanner = [NSScanner scannerWithString:html]; while ([theScanner isAtEnd] = = NO) {/ / find start of tag [theScanner scanUpToString:@ "<" intoString:NULL] end of tag; / / find [theScanner scanUpToString:@ > "; intoString:& text]; the found tag with a / / replace / / space (you can filter Multi-Spaces out later if you wish HTML HTML stringByReplacingOccurrencesOfString: [NSString) = [stringWithFormat:@"%@> "text] withString:@", "MidStrTitle";} = HTML; re Turn HTML;}

So we have to deal with the HTML tag, so the question is, if we have to deal with a lot of HTML tags, how should we deal with it, this is the purpose of this article.

TFHpple Library

If we want to get the following is a HTML source code, the contents of a label it, part of the source code is as follows, here is only part of the label

< title> statement on APP < /title> < /head> < body> < h3> statement on APP < /h3> < p> test test test is just just just just just just test test test test test test test just just just just just just just test test test just just just test test test test test test test is just only < /p> < p> test test test is just just just just just just test test test test test test test is just just just just just just test test test test test test test is just just just just just test test test < /p> < h3> /h3> < contact us; < p> if you use the APP has encountered any problems or new ideas, can To contact us. The following is the way to contact us: < /p> < ul> < li> test test test is just just just just just just test test test test test test test is just just just just just just test test test test test test test is just just just just just test test test < /li> li> < E-mail:; xxxxxxxxxxxxxxxxxxxxxxxxxx< /li> < li> phone: xxxxxxxxxxxxxxxxxx< /li> < /ul> < h3> /h3> < for < p> first of all, thanks to the vast number of users of the company's support and encouragement, thank you for the way we support.< /p> < p> just just just test is to test the test test test test test is just just just just just just test test test test test only Just just just just test test test test test test test just just just just test < /p> < h3> /h3> copyright < < p> test test test is just just just just just just test test test test test test test is only just just just just just just test test test test only just just just test test test test test test is only < /p> < /body> < /html>

Here if I want to take a specific label of the content we need to use the library today.

Environment configuration

Import static library

IOS analysis of HTMl tags and the development of some of the pit
import static library.Png

then adds the imported static library to the path of the file

IOS analysis of HTMl tags and the development of some of the pit
add library file paths.png

and then import the TFHpple library on it.

Method of use 1, a simple form of labeling method

Direct code

NSString *resourcePath = [[NSBundle mainBundle] resourcePath]; NSString *filePath =[resourcePath stringByAppendingPathComponent:@ "about.html"]; NSData *data =[[NSData alloc]initWithContentsOfFile:filePath]; NSString *result = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; //data string to print is not garbled NSLog (@ "------%@", result); TFHpple *Hpple = [[TFHpple alloc]initWithHTMLData:data]; / / test 1: get simple title NSArray *array =[Hpple searchWithXPathQuery:@ "//title"]; / / get to the title of title for (TFHppleElement *HppleElement in array (NSLog) {@ 1 test to label content: -% @ ", HppleElement.text);}

We first get the file to the local about.html, and then converted to data, plus UTF8 encoding conversion as we can see the content as follows:

IOS analysis of HTMl tags and the development of some of the pit
run results diagram

on the part of the original label for the HTML file, the next part of the label for us to go to the “title” content.
so we got what we wanted.

Method of use 2: have their own attributes of the label value

The above HTML file because of the limitations of the content is not convenient for us, for example, we use the HTML tag source code is as follows:

< html> < head> < meta http-equiv= "Content-Type" content= "text/html; charset=UTF-8 > < title> hello< /title> < /head> < body> Hello world < span> < /span> < unordered list" disc "; UL type= > < li> apple < /li> < li> /li> < fruit < li> < /li> < peach; /ul> < UL type=" circle "> < li> /li> < 2< li> 2< /li > fruit; < li> 2< /li> peach; < /ul> < /body> < /html>

We want to get to the unordered list node attributes for the type attribute value of disc label content, obviously the above method has been unable to meet our needs, the old rules on the first code:

/ / test 2: in order to obtain more detailed content of NSString *resourcePath mainBundle] = [[NSBundle resourcePath]; NSString *filePath =[resourcePath stringByAppendingPathComponent:@ "first.html"]; NSData *data =[[NSData alloc]initWithContentsOfFile:filePath]; NSString *result = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; //data string to print is not garbled NSLog (@ "------%@", result); TFHpple *Hpple = [[TFHpple alloc]initWithHTMLData:data]; NSArray *array =[Hpple searchWithXPathQuery:@ "//ul"]; for (TFHppleElement *HppleElement in array) {if ([HppleElement.attributes[@ "type"]isEqualToString:@ "disc"]) {/ / find the UL label node attribute value of the type attribute to the disc array NSArray *array2 [HppleElement searchWithXPathQuery:@ = "//li"]; for (TFHppleElement *HppleElement2 in array2 (NSLog) {@ 2 test to label content: -% @ ", HppleElement2.text);}}}

The use of the focus on the following if statements in the judgment

If ([HppleElement.attributes[@ "type"]isEqualToString:@ "disc") {}

If there is such a condition, according to the BOOL value, go to the following label “Li” content
run as follows:

IOS analysis of HTMl tags and the development of some of the pit
method two run results

Some pits encountered in the project

We finished the label and can not end, it is difficult to have time to write some of the small problems encountered.

Custom keyboard

Custom keyboard can also be regarded as an old problem, due to the different circumstances, we may need to define a fully custom keyboard, to adapt to the needs of a variety of wonderful. Recently read the code when it was found that different people do not really write the same way, here is a simple way to think of a person.
UITextField inputView attributes, the use of examples as follows:

KkeyboardView = [[UIView alloc] initWithFrame:CGRectMake (0, 0, 320, 260)]; kkeyboardView.backgroundColor = [UIColor lightGrayColor]; [self setUpMyKeyBoard2]; self.textfiled.inputView = kkeyboardView;

Create a View and then as a UITextField of the inputView, this time you can completely customize the custom keyboard you want, and you come to say what you want on the keyboard.

IOS analysis of HTMl tags and the development of some of the pit
to talk about
UIScrollView rolling problem

The day before yesterday, colleagues said a strange question, said lazy loading also wrote, the layout has been done, the offset is also set up, in other pages do a good job, and this page is rolling to try not to move. The old driver also overturned love, but how to deal with the problem.

- (void) viewDidLayoutSubviews {_BaseScore.contentSize = CGSizeMake (SCREEN_WIDTH, 568 - 44);}

A toss after he gave him the method to write the offset in this way you can scroll, colleagues can not help but say older, found that their memory is getting worse, ha ha, a group of such colleagues really work more fun.

Postscript

Mainly introduced today was so much I want to be a buddy to help, if you have time to write about the next time, the project used, end businesses generate two-dimensional code collection, end users to scan two-dimensional code payment, and refresh, state judge, scanning the two-dimensional code generating the invitation code and a series of two-dimensional code related functions. There is no welcome to write the local point of view, God light spray, ha ha.

Supplement:

Add two simple methods:

  • The attributedString property NSAttributedString * Label attrStr alloc] initWithData:[h5str dataUsingEncoding:NSUnicodeStringEncoding] options:@{= [[NSAttributedString NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType documentAttributes:nil error:nil] [selflab setAttributedText:attrStr]};
  • The use of Web view [[UIWebView alloc] initWithFrame:CGRectMake (_webView = 100, 100, 100, 100)]; NSString *html_str = [NSString stringWithFormat:@ < div style=’color:red’> 666666< /div> test, @ “% @” air waves “,” @ 2020-01-00 “]; [self.webView loadHTMLString:html_str baseURL:nil]; [self.view addSubview:self.webView];