iOS development using UIWebView for graphics and text blending
Time 2014-08-15 13:45:35 CSDN Blog
Original http://blog.csdn.net/wildcatlele/article/details/38583731
Theme UIWebView
At the beginning of the first news project, do not know the use of UIWebView to achieve the effect of illustrations, so the most stupid way to use the TableView to solve the news browsing. When a little project experience is known to be possible with UIWebView or Coretext implementations, there has been no attempt. When the last interview was asked how to use WebView for the news display, then the circle. Come back, check the data found so simple, the original transfer from http://blog.csdn.net/wildcatlele/article/details/38583731 reprint please specify.
1. Okay, nonsense. First introduce, the main function of this demo, you can display the news content (picture and text mixed), links can be clicked to the original news page;
2. Look at the effect below:
Steps:
0. Create two view control bodies
Create a new project, and then create a two controller Lvesviewcontroller and Lvesoriginalcontroller
Add one UIWebView to Self.view, respectively.
1. Remove the webview when rolling, the white edge of the upper and lower.
-(void) Clearwebviewbackground: (UIWebView *) WebView
{
UIWebView *web = WebView;
For (ID v in web.subviews) {
if ([V Iskindofclass:[uiscrollview class]]) {
[v Setbounces:no];
}
}
}
2. Setting up the agent
2. Set up Proxy
self.mywebview.delegate=self;
3. Add a view that loads WebView
#pragma mark Load WebView
-(void) loadmywebview{
NSString *[email protected] "Han cold" after the "Wonderful gold suction 3 cheats";
NSString *linkstr=[nsstring stringwithformat:@ "<a href= '%@ ' > My blog </a> <a href= '%@ ' > Original </a>" @ "Http://blog.csdn.net/wildcatlele", @ "http://jincuodao.baijia.baidu.com/article/26059"];
NSString *[email protected] "Han cold after will be endless" the ability to absorb gold I am surprised! The August 12 film box office has successfully breached the 600 million mark. And the amount of the row is still more than 10, to the daily rate of tens of millions of steady march to 700 million. ";
NSString *[email protected] "You know," After will be "is not the mainstream genre film, is a literary film. Unlike "little Times", is a mainstream business idol movie. ";
NSString *image1=[nsstring stringwithformat:@ " ', @" http://nvren.so/ Uploads/allimg/c140801/140dr4554l40-yb9.jpg "];
NSString *image2=[nsstring stringwithformat:@ " ', @"/http f.hiphotos.baidu.com/news/w%3d638/sign=78315beeb1fb43161a1f797918a44642/ 2934349b033b5bb58cb61bdb35d3d539b600bcb5.jpg "];
NSString *[email protected] "It's so wonderful!" Some people say that this is the Chinese film market dividend, is the success of fan movies. However, there is an investment of 30 million of the fan movie "I am Me", there are stars, production is also good, basically a fiasco. ";
NSString *[email protected] "after will life" sell is not a good story, is a superiority. Especially for 80, the population, you have not found, see "After will be endless" than see "small times 3" there is a clear sense of superiority. Although the story is general, but many people after reading, will be on Weibo, on the photo. Therefore, it is not the breadth but the depth of an ethnic group. <br>\
\
Very vicious, worthy of reference for everyone. Han cold "after will be endless" and what secret weapons, welcome "after will be endless" team or related party explosive material, direct message can be sent Li Vanchang autographed "Participation Feeling" a book. ";
Initialization and HTML strings
NSString *htmlurlstr=[nsstring stringwithformat:@ "<body style= ' background-color: #EBEBF3 ' >
[Self.mywebview loadhtmlstring:htmlurlstr Baseurl:nil];
}
4. Implementing the Proxy method (handling connection click events)
-(BOOL) WebView: (UIWebView *) WebView shouldstartloadwithrequest: (nsurlrequest *) Request Navigationtype: ( Uiwebviewnavigationtype) navigationtype{
NSString *urlstr=request. url.absolutestring;
NSLog (@ "URL:%@", URLSTR);
Empty, load this page for the first time
if ([Urlstr isequaltostring:@ "About:blank"]) {
return YES;
}
Set the view controller after the click
Lvesoriginalcontroller *originalc=[[lvesoriginalcontroller alloc] init];
ORIGINALC.ORIGINURL=URLSTR; Setting up a request connection
Jump to the post-click Controller and load WebView
[Self.navigationcontroller Pushviewcontroller:originalc Animated:yes];
return NO;
}
Set bottom scrolling does not bounce back
-(void) Webviewdidfinishload: (UIWebView *) webview{
Nsinteger height = [[WebView stringbyevaluatingjavascriptfromstring:@ "document.body.offsetHeight;] Intvalue];
nsstring* JavaScript = [NSString stringwithformat:@ "Window.scrollby (0,%d);", height];
[WebView Stringbyevaluatingjavascriptfromstring:javascript];
}
5. Implement Origainalviewcontroller
//
Lvesoriginalcontroller.m
Webviewdemo
//
Created by Lves Li on 14-8-15.
Copyright (c) 2014 lves. All rights reserved.
//
#import "LvesOriginalController.h"
@interface Lvesoriginalcontroller ()
{
UIWebView *_webview;
}
@end
@implementation Lvesoriginalcontroller
-(ID) Initwithnibname: (NSString *) Nibnameornil Bundle: (NSBundle *) Nibbundleornil
{
self = [super Initwithnibname:nibnameornil Bundle:nibbundleornil];
if (self) {
Custom initialization
}
return self;
}
-(void) viewdidload
{
[Super Viewdidload];
Set Title
[Email protected] "original";
_webview=[[uiwebview alloc] init];
_webview.frame=[[uiscreen Mainscreen] bounds];
[Self.view Addsubview:_webview];
[_webview loadrequest:[nsurlrequest Requestwithurl:[nsurl URLWITHSTRING:SELF.ORIGINURL]];
}
@end
iOS development using UIWebView for graphics and text blending