您的位置:首页 > 移动开发

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';"];
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: