简体   繁体   English

在Webview中缩放html内容

[英]Scale html content in Webview

I load a local HTML template into a WebView . 我将本地HTML模板加载到WebView My webview's frame size is very small (250X300). 我的webview的框架尺寸非常小(250X300)。 I use this to show only a preview. 我用这个只显示预览。 The problem is, I can't see the entire html content in the webview. 问题是,我无法在webview中看到整个html内容。 I can see only the top left. 我只看到左上角。 I have to scroll to see the entire content. 我必须滚动才能看到整个内容。 So is there any possiblity to set the scale or zoom factor for the webview? 那么为webview设置比例或缩放因子是否有可能?

If it is good enough for you to zoom text only, then use the methods -[WebView makeTextSmaller:] and -[WebView makeTextLarger:] and to reset the text size -[WebView makeTextStandardSize:] as documented for WebView . 如果它足以让你只缩放文本,那么使用方法-[WebView makeTextSmaller:]-[WebView makeTextLarger:]并重置文本大小-[WebView makeTextStandardSize:]WebView These methods correspond to "Cmd -" and "Cmd +" and "Cmd 0" in Safari. 这些方法对应于Safari中的“Cmd - ”和“Cmd +”和“Cmd 0”。

If you want to scale the whole content of a WebView including images I use the following method: 如果要扩展包含图像的WebView的整个内容,请使用以下方法:

- (void) zoomWebView:(WebView *)aWebView withFactor:(float)fac
{
    NSString *script = @"document.body.style.zoom";
    float oldFac = [[aWebView stringByEvaluatingJavaScriptFromString:script] floatValue];
    if( oldFac==0 ){ oldFac = 1.0; }
    NSString *res = [aWebView stringByEvaluatingJavaScriptFromString:
            [NSString stringWithFormat:@"%@='%1.2f';", script, (oldFac*fac)]];
    [aWebView setNeedsDisplay:YES];
}

经过一番游戏,我得到了答案。

    [[[[[previewWeb mainFrame] frameView] documentView] superview] scaleUnitSquareToSize:NSMakeSize(.5, .5)];

You can use the undocumented 您可以使用未记录的

- (void)_setZoomMultiplier:(float)m isTextOnly:(BOOL)isTextOnly

Note : That this may cause problems with submissions to the App Store. 注意 :这可能会导致提交到App Store时出现问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM