webView基本用法
2013-05-07 14:43
525 查看
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UIWebView *webView = [[[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)] autorelease];
//使用loadHTMLString()方法显示网页内容 使用此方法是如果html非完整需要进行转换!!
[webView loadHTMLString:[self getHtmlString] baseURL:nil];
[self.view addSubview:webView];
}
//读取html文件内容
- (NSString *)getHtmlString{
//文件路径
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"index" ofType:@"html"];
NSString *contents = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];
return contents;
}
1. 给他设置一个代理类 UIWebView needs a delegate to reset it's size accoreding to it's content.
2. 代理类中实现如下方法 Implish "- (void)webViewDidFinishLoad:(UIWebView *) webView" in the delegate:
- (void)webViewDidFinishLoad:(UIWebView *) webView
{
CGSize actualSize = [webView sizeThatFits:CGSizeZero];
CGRect newFrame = webView.frame;
newFrame.size.height = actualSize.height;
webView.frame = newFrame;
}
3. OK.
二.使用javascipt获得可是可视区域宽高
@implementation UIWebView (SFHFStringMetrics)
- (CGSize) sizeOfDocument {
NSString *javascriptString = @"\"{\" + document.documentElement.clientWidth + \", \" + document.documentElement.clientHeight + \"}\"";
NSString *sizeString = [self stringByEvaluatingJavaScriptFromString: javascriptString];
if (!sizeString || [sizeString length] < 1) {
returnCGSizeZero;
}
returnCGSizeFromString(sizeString);
}
5.禁止长按事件
[webView stringByEvaluatingJavaScriptFromString:@"document.documentElement.style.webkitUserSelect='none';"];
[webView stringByEvaluatingJavaScriptFromString:@"document.body.style.webkitTouchCallout='none';"];
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UIWebView *webView = [[[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)] autorelease];
//使用loadHTMLString()方法显示网页内容 使用此方法是如果html非完整需要进行转换!!
[webView loadHTMLString:[self getHtmlString] baseURL:nil];
[self.view addSubview:webView];
}
//读取html文件内容
- (NSString *)getHtmlString{
//文件路径
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"index" ofType:@"html"];
NSString *contents = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];
return contents;
}
1. 给他设置一个代理类 UIWebView needs a delegate to reset it's size accoreding to it's content.
2. 代理类中实现如下方法 Implish "- (void)webViewDidFinishLoad:(UIWebView *) webView" in the delegate:
- (void)webViewDidFinishLoad:(UIWebView *) webView
{
CGSize actualSize = [webView sizeThatFits:CGSizeZero];
CGRect newFrame = webView.frame;
newFrame.size.height = actualSize.height;
webView.frame = newFrame;
}
3. OK.
二.使用javascipt获得可是可视区域宽高
@implementation UIWebView (SFHFStringMetrics)
- (CGSize) sizeOfDocument {
NSString *javascriptString = @"\"{\" + document.documentElement.clientWidth + \", \" + document.documentElement.clientHeight + \"}\"";
NSString *sizeString = [self stringByEvaluatingJavaScriptFromString: javascriptString];
if (!sizeString || [sizeString length] < 1) {
returnCGSizeZero;
}
returnCGSizeFromString(sizeString);
}
5.禁止长按事件
[webView stringByEvaluatingJavaScriptFromString:@"document.documentElement.style.webkitUserSelect='none';"];
[webView stringByEvaluatingJavaScriptFromString:@"document.body.style.webkitTouchCallout='none';"];
相关文章推荐
- (一)WebView实践之基本用法
- Android基础入门教程——7.5.1 WebView(网页视图)基本用法
- WebView(原生)与Js 交互 基本与高级用法
- WebView的基本用法以及和js交互
- 【Android基础知识】WebView基本用法
- webview在android端基本用法(三)
- Android---WebView基本用法
- Android基础—WebView(网页视图)基本用法
- 安卓网络编程-WebView基本用法
- Android基础入门教程——7.5.1 WebView(网页视图)基本用法
- WebView使用解析(一)之基本用法
- android WebView, WebChromeClient和WebViewClient加载网页基本用法
- android WebView, WebChromeClient和WebViewClient加载网页基本用法
- Android WebView的基本用法
- android WebView, WebChromeClient跟WebViewClient加载网页基本用法
- WebView(网页视图)基本用法
- WebView基本用法
- WebView基本用法
- Android基础学习总结(十)——WebView的基本用法总结
- Android WebView 基本用法