UIWebView捏缩放以增加字体大小?

时间:2013-11-05 20:25:14

标签: ios uiwebview

如何在缩放缩放时使UIWebView增加字体大小?

如果我设置scalesPageToFit = YES,则缩放缩放将缩放网页,但宽度将大于设备宽度。我希望宽度仍然是设备宽度,但字体应该更大。

1 个答案:

答案 0 :(得分:2)

没有纯粹的客观C方式可以让你实现这一目标。但是,您可以使用一些JS技巧来增加字体大小。尝试使用:

//determine current "fontSize" with the help of some pinch gesture recognizer
NSString* contentHeight = [webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"scaleFont(%d)",fontSize]];
webView.scrollView.contentSize = CGSizeMake(webView.scrollView.contentSize.width, [contentHeight floatValue]);

在你的html页面中,创建一个类似于下面的JS函数:

function scaleFont(fontSize) {
    document.getElementsByTagName('body')[0].style.webkitTextSizeAdjust=fontSize;
    return document.documentElement.offsetHeight;
}

您仍然需要通过一些捏合手势识别器或类似的东西来确定Objective-C代码中的预期fontSize。还请确保使用scalesPageToFit = NO禁用UIWebView的默认缩放功能;并删除/禁用html文档中的任何“viewport”元标记。

希望它有所帮助。

相关问题