Scale WebView in Cocoa

2019-01-15 13:37发布

How can I scale the content of a WebView?

4条回答
Juvenile、少年°
2楼-- · 2019-01-15 14:03

Javascript is your bestfriend:

 [_webview stringByEvaluatingJavaScriptFromString:@"document.documentElement.style.zoom = \"0.5\""];
查看更多
【Aperson】
3楼-- · 2019-01-15 14:10

You need to call -scaleUnitSquareToSize: on the document view of the individual frame that you want to resize, for example the main frame:

[[[[webView mainFrame] frameView] documentView] scaleUnitSquareToSize:NSMakeSize(1.5, 1.5)];
[[[[webView mainFrame] frameView] documentView] setNeedsDisplay:YES];
查看更多
走好不送
4楼-- · 2019-01-15 14:25

Or you can try the following:

- (IBAction)takeZoom:(NSSlider *)sender {
    WebView *aWebView = [[window.contentView subviews] lastObject];
    NSView *clipView = [[[[aWebView mainFrame] frameView] documentView] superview];
    float scale = pow( 4, [sender floatValue] )/(clipView.frame.size.width/clipView.bounds.size.width);
    [clipView scaleUnitSquareToSize:NSMakeSize(scale, scale)];
    [clipView setNeedsDisplay:YES];
}
查看更多
一夜七次
5楼-- · 2019-01-15 14:27

A WebView is an NSView, so one way would be to send it a scaleUnitSquareToSize: message.

查看更多
登录 后发表回答