When we program the details page of some interface (like news app, group purchase app, etc.), sometimes the detail page needs different typesetting effect after a period of time, if the layout of the view page is re-arranged, it may waste a lot of time and cause some trouble. Because HTML can already be embedded on iOS, so we can use HTML template, nested into the UIWebView to use, if you need a new page layout, simply need to replace the HTML template, convenient.
Because I have no contact with HTML programming, basically the content on the HTML template is completely ignorant, in fact, the use of HTML template only need to know its corresponding label, complete the replacement can be. The HTML template I used consists of three files: an HTML file (used to write the contents of the page), a CSS file (used to set up the page layout), and a JS file (where the page trigger event is handled). These three pages I feel like an iOS MVC development pattern. The label content we need to replace is primarily in the HTML file (code one).
Code One
<HTML><Head> <Metaname= "Viewport"content= "Target-densitydpi=device-dpi,width=320,user-scalable=no"/> <Linkhref= ' Content.css 'rel= ' stylesheet '/> <Scriptsrc= "Content.js"></Script></Head><Body> <DivID= "Title_section"class= "Font_small"><DivID= ' title '>{{title}}</Div><DivID= ' subtitle '>{{Source}}<spanAria-hidden= "true">{{Ptime}}</span></Div></Div><DivID= "Body_section"Lang= "zh"><P><Divclass= ' PRE '>{{Digest}}</Div></P>{{body}}<PAlign= "Right">(Editor: {{EC}})</P><PAlign= "Center"><ahref= "Source:///{{source_url}}"class= "Border_link">View Original<span></span></a></P></Div></Body></HTML>
By sending a network request, the returned JSON data is parsed and a nsdictionary is obtained by assigning a value (such as code two, three) to the corresponding tag in the HTML (Code four): The Returned nsstring, Load to WebView on the loadhtmlstring, and then add the template path of HTML (code five), it can be achieved.
Code two
@interface**** * * * * SOURCEURL;@property ( Nonatomic, Strong) Nsarray *images; + (Instancetype) infofromdict: (Nsdictionary *) dict;
Code Three
+ (Instancetype) infofromdict: (Nsdictionary *) dict{WebInfo*info =[[WebInfo alloc] init]; Info.title= [Dict Objectforkey:@"title"]; Info.source= [Dict Objectforkey:@"Source"]; Info.ptime= [Dict Objectforkey:@"Ptime"]; Info.digest= [Dict Objectforkey:@"Digest"]; Info.body= [Dict Objectforkey:@"Body"]; Info.ec= [Dict Objectforkey:@"EC"]; Info.sourceurl= [Dict Objectforkey:@"Source_url"]; Nsarray*images =[dict Objectforkey:@"img"]; Info.images=[Imageinfo arrayfromarray:images]; returninfo;}
Code Four
//HTML Tags#defineHtmlBody @ "{Body}}"#defineHTMLTitle @ "{{title}}"#defineHtmlsource @ "{{source}}"#defineHtmlptime @ "{{ptime}}"#defineHtmldigest @ "{{Digest}}"#defineHtmlsourceurl @ "{{Source_url}}"#defineHtmlImage @ "<p> '-(NSString *) HTMLConvert: (WebInfo *) info{NSString*file = [[NSBundle mainbundle]pathforresource:@"Content_template2"OfType:@"HTML"]; NSString*html =[[NSString alloc]initwithcontentsoffile:file encoding:nsutf8stringencoding Error:nil]; HTML=[HTML stringbyreplacingoccurrencesofstring:htmlbody withString:Info.body]; HTML=[HTML stringbyreplacingoccurrencesofstring:htmltitle withString:Info.title]; HTML=[HTML stringbyreplacingoccurrencesofstring:htmlptime withString:Info.ptime]; HTML=[HTML stringbyreplacingoccurrencesofstring:htmldigest withString:Info.digest]; HTML=[HTML Stringbyreplacingoccurrencesofstring:htmlsource withString:Info.source]; returnhtml;}
Code Five
NSString *urlpath = [[NSBundle mainbundle]pathforresource:@ 'content_template' ofType: @" HTML " ]; *htmlstring =* web = [[UIWebView alloc]initwithframe:self.view.frame]; Web. delegate = self ; [Self.view Addsubview:web]; [Web loadhtmlstring:htmlstring baseurl:[nsurl Urlwithstring:urlpath];
Create a detail page using an HTML template