In the previous blog, I learned how to open-source Chinese iOS client-(12) about the user logon interface and loginView. in the xib layout, we can see a UIWebView control, but it does not load a webpage, but displays some text information, including some URL links, click this link to go to the website, or click a text tag to achieve the same effect.
650) this. width = 650; "src =" http://img.blog.csdn.net/20130525205431479 "_ xhe_src =" http://img.blog.csdn.net/20130525205431479 "style =" max-width: 98%; "/>
Let's take a look at another ViewController. There is a NewDetail class under the News/tab. When we look at the xib class of this ViewController class, we see the WebView control. Similarly, it is not used to load a webpage, it is used as the view layout, and webView controls are used as the layout engine in the Open Source Chinese iOS client source code, the SinglePost, SoftwareDeatil, and BlogDetail classes under the tab directory of the project file are all used in this way. The NewDetail class is used to load details that display a piece of information;
650) this. width = 650; "src =" http://img.blog.csdn.net/20130527141735228 "_ xhe_src =" http://img.blog.csdn.net/20130527141735228 "style =" max-width: 98%; "/>
650) this. width = 650; "src =" http://img.blog.csdn.net/20130527142755997 "_ xhe_src =" http://img.blog.csdn.net/20130527142755997 "style =" max-width: 98%; "/>
Then let's take a look at the content displayed on the WebView layout interface. When you select a piece of information in the comprehensive homepage, you want to view the details of the consultation, and then view some Interface Effects:
650) this. width = 650; "src =" http://img.blog.csdn.net/20130527143733806 "_ xhe_src =" http://img.blog.csdn.net/20130527143733806 "style =" max-width: 98%; "/> 650) this. width = 650; "src =" http://img.blog.csdn.net/20130527143737541 "_ xhe_src =" http://img.blog.csdn.net/20130527143737541 "style =" max-width: 98%; "/>
Maybe we want to ask what to use the WebView layout. The normal view can also be displayed. The display text can be satisfied with the ULabel UITextView display image and UIImageView, and then let's look down...
There are some special font colors. When you click them, some jump to another view, and some jump to a website to respond to user interaction; the following is an example of how to capture the title of a TNW or related article:
650) this. width = 650; "src =" http://img.blog.csdn.net/20130527144149659 "_ xhe_src =" http://img.blog.csdn.net/20130527144149659 "style =" max-width: 98%; "/> 650) this. width = 650; "src =" http://img.blog.csdn.net/20130527144153673 "_ xhe_src =" http://img.blog.csdn.net/20130527144153673 "style =" max-width: 98%; "/>
Some people may already know that these are all rich texts in iOS. Indeed, this is the use of Web views to display rich texts. Can rich texts be used in common views? Certainly, there are some third-party libraries such as RTLabel to support the use of rich text. In iOS 6, UIKit also provides rich text support. It may be okay to display simple text labels, but it may be difficult to layout some complicated views, just like adding a hyperlink to a word in a piece of text, click two words to jump to a Web page. It is not difficult to use only two words, but what should I do if I use the same xib interface layout for multiple pieces of information? We don't know which words need rich text with hyperlinks? At this time, webView has become the best solution. When we open a Web page, such as hao123 navigation, there are many text links on it. Click these words to jump to the corresponding official website, and WebView can load HTML code, it is also very simple, but it is a little difficult in View;
Well don't talk nonsense, first look at WebView how to load a piece of information content, first look at a piece of information, return xml format http://www.oschina.net/action/api/news_detail? Id = 40840. parse the data in the body node as HTML code, which is loaded by WebView. Other headers have corresponding URLs, format the parsed title and url into HTML code and add webView;
Check the ViewDidLoad method first.
-(Void) viewDidLoad {[super viewDidLoad]; self. tabBarItem. title = @ ""; self. tabBarItem. image = [UIImage imageNamed: @ "detail"]; // remove the background color of WebView [Tool clearWebViewself. webView]; self. singleNews = [[SingleNews alloc] init]; self. navigationController. title = @ ""; self. webView. delegate = self; [self. webView loadHTMLString: @ "" baseURL: nil]; if ([Config Instance]. isNetworkRunning) {MBProgressHUD * Hud = [[MBProgressHUD alloc] initWithView: self. view]; [Tool showHUD: @ "loading" andView: self. view andHUD: hud]; NSString * url = [NSString stringWithFormat: @ "% @? Id = % d ", Token, newsID]; [[AFOSCClient sharedClient] getPath: url parameters: nil success: ^ (AFHTTPRequestOperation * operation, id responseObject) {[Tool getOSCNotice2: operation. responseString]; [hud hide: YES]; self. singleNews = [Tool readStrNewsDetail: operation. responseString]; if (self. singleNews = nil) {[Tool ToastNotification: @ "loading failed" andView: self. view andLoading: NO andIsBottom: NO]; return;} [self loadData: self. singleNews]; // if there is a network, cache it if ([Config Instance]. isNetworkRunning) {[Tool saveCache: 1 andID: self. singleNews. _ id andString: operation. responseString] ;}} failure: ^ (AFHTTPRequestOperation * operation, NSError * error) {[hud hide: YES]; if ([Config Instance]. isNetworkRunning) {[Tool ToastNotification: @ "error network not connected" andView: self. view andLoading: NO andIsBottom: NO] ;}} else {NSString * value = [Tool getCache: 1 andID: newsID]; if (value) {self. singleNews = [Tool readStrNewsDetail: value]; [self loadData: self. singleNews];} else {[Tool ToastNotification: @ "the error network is not connected" andView: self. view andLoading: NO andIsBottom: NO] ;}}
There are two main methods in ViewDidLoad:
1. [ToolclearWebViewBackground: self. webView]; remove the background color of WebView. When displaying data, it is not considered a WebView but a common View;
2. [selfloadData: self. singleNews]; format the processing string and convert it to HTML format, such as setting the html background color font size. It is useless for HTML and can be understood a little,
-(Void) loadData :( SingleNews *) n {[self refreshFavorite: n]; // notification to modify the number of news comments ication_commentcount * notification = [[Notification_CommentCount alloc] initWithParameters: n. commentCount]; [[nsicationicationcenter defacenter center] postNotificationName: icationication_detailcommentcount object: notification]; // news is mainly used for Weibo sharing [Config Instance]. jsonobject = [[jsonobject alloc] initWithParameters: n. title andUrl: n. url]; // Control update NSString * author_str = [NSString stringWithFormat: @ "<a href = 'HTTP: // my.oschina.net/u/mongod' >%@ </a> published on % @", n. authorid, n. author, n. pubDate]; NSString * software = @ ""; if ([n. softwarename iswartostring: @ ""] = NO) {software = [NSString stringWithFormat: @ "<div id = 'osschina _ soft' style = 'margin-top: 8px; color: # FF0000; font-size: 14px; font-weight: bold '>