【问题标题】:UIWebView pinch zoom to increase font size?UIWebView捏缩放以增加字体大小?
【发布时间】:2013-11-05 20:25:13
【问题描述】:

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

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

【问题讨论】:

    标签: ios uiwebview


    【解决方案1】:

    没有纯粹的客观 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 代码中的预期字体大小。还请确保使用 scalesPageToFit = NO 禁用 UIWebView 的默认缩放功能;并删除/禁用 html 文档中的任何“视口”元标记。

    希望对你有帮助。

    【讨论】:

    • 这是一个很好的答案。然而,将手势识别器添加到 Web 视图通常会出现问题。
    猜你喜欢
    • 2023-03-11
    • 1970-01-01
    • 2010-10-10
    • 1970-01-01
    • 2014-08-03
    • 2013-07-01
    • 2018-04-06
    • 2013-03-20
    • 1970-01-01
    相关资源
    最近更新 更多