我想在Web视图中设置简单的html内容,然后根据其内容调整大小.
要在Web视图中设置简单的html内容,我使用了这段代码,它运行正常:
[[myWebView mainFrame] loadHTMLString:webViewContents baseURL:baseURLFramed];
现在,如果内容超过其实际大小,则它将显示在Web视图中,同时显示其中的垂直和水平滚动条.我想设置一些默认宽度并根据其内容管理高度,以便既不出现水平也不出现垂直滚动.
有人能建议我一些解决方案吗?
谢谢,
Miraaj
您可以注册为WebView
框架加载委托,当页面加载时,您可以询问主框架的文档视图的大小并调整大小WebView
.确保关闭框架上的滚动条,否则您将遇到问题.
请注意,某些页面可能非常大,当我测试daringfireball.net时,它的高度为17612点,这显然太大而无法在屏幕上显示.
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification { //set ourselves as the frame load delegate so we know when the window loads [webView setFrameLoadDelegate:self]; //turn off scrollbars in the frame [[[webView mainFrame] frameView] setAllowsScrolling:NO]; //load the page NSURLRequest* request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://daringfireball.net"]]; [[webView mainFrame] loadRequest:request]; } //called when the frame finishes loading - (void)webView:(WebView *)sender didFinishLoadForFrame:(WebFrame *)webFrame { if([webFrame isEqual:[webView mainFrame]]) { //get the rect for the rendered frame NSRect webFrameRect = [[[webFrame frameView] documentView] frame]; //get the rect of the current webview NSRect webViewRect = [webView frame]; //calculate the new frame NSRect newWebViewRect = NSMakeRect(webViewRect.origin.x, webViewRect.origin.y - (NSHeight(webFrameRect) - NSHeight(webViewRect)), NSWidth(webViewRect), NSHeight(webFrameRect)); //set the frame [webView setFrame:newWebViewRect]; //NSLog(@"The dimensions of the page are: %@",NSStringFromRect(webFrameRect)); } }