【问题标题】:UIWebView HTML font size issueUIWebView HTML 字体大小问题
【发布时间】:2013-05-05 20:35:44
【问题描述】:

我在我的一个 iPhone 应用程序的 UIWebView 中显示 HTML 内容。

查看以下 2 张图片:

https://dl.dropboxusercontent.com/u/68104003/Screen%20Shot%202013-05-11%20at%2011.47.02%20AM.png https://dl.dropboxusercontent.com/u/68104003/Screen%20Shot%202013-05-11%20at%2011.47.28%20AM.png

我为这两个标题使用了相同的 HTML 代码,但它在屏幕上显示的字体大小不同。

以下是我使用的 HTML 代码:

<span style="font-size:19px;"><strong><span style="font-family:times new roman,times,serif;">4.6&nbsp;&nbsp;Lorem Ipsum is simply</span></strong></span></br>

<span style="font-size:19px;"><strong><span style="font-family:times new roman,times,serif;">4.4&nbsp;&nbsp;Type and Scrambled</span></strong></span></br>

请帮忙。

【问题讨论】:

    标签: ios uiwebview


    【解决方案1】:

    UIWebView 支持缩放,这自然会影响类型的大小。很可能当您看到较大的字体时,您只是进一步放大了,可能是因为两个页面的宽度不同。

    【讨论】:

    • 当我们缩放时肯定会影响大小,但是当我不缩放页面时,它也会显示不同的字体大小。这是一个图书应用程序,所以我使用了 UIPageViewController,我在其中显示了 UIWebView,页面宽度相同。
    • @Reena 我知道网络视图可能相同,但由 HTML 确定的页面宽度可能不同。 UIWebView 将尝试适应整个 HTML 页面,因此如果您在一个页面上有一些较宽的内容,而在另一页面上没有,则具有较宽内容的页面上的文本会显得更小。
    【解决方案2】:

    我使用下面的代码来修复字体大小和文本对齐问题。希望对你有所帮助。

    - (void)viewDidLoad
    {   [super viewDidLoad];
    currentTextSize = 100;
    webview.userInteractionEnabled=YES;
    webview.delegate=self;
    webview.scalesPageToFit=YES;
    webview.scrollView.pagingEnabled = YES;}
    
    -(void)webViewDidFinishLoad:(UIWebView *)webView{
    NSString *varMySheet = @"var mySheet = document.styleSheets[0];";
    NSString *addCSSRule =  @"function addCSSRule(selector, newRule) {"
    "if (mySheet.addRule) {"
    "mySheet.addRule(selector, newRule);"                               // For Internet Explorer
    "} else {"
    "ruleIndex = mySheet.cssRules.length;"
    "mySheet.insertRule(selector + '{' + newRule + ';}', ruleIndex);"   // For Firefox, Chrome, etc.
    "}"
    "}";
     NSString *insertRule2 = [NSString stringWithFormat:@"addCSSRule('p', 'text-align: justify;')"];
    NSString *insertRule1 = [NSString stringWithFormat:@"addCSSRule('html', 'padding: 0px; height: %fpx; -webkit-column-gap: 0px;')", webView.frame.size.height];
    
    NSString *setTextSizeRule = [NSString stringWithFormat:@"addCSSRule('body', '-webkit-text-size-adjust: %d%%;')", currentTextSize];
     [webView stringByEvaluatingJavaScriptFromString:varMySheet];
    [webView stringByEvaluatingJavaScriptFromString:addCSSRule];
    [webView stringByEvaluatingJavaScriptFromString:insertRule1];
    [webView stringByEvaluatingJavaScriptFromString:insertRule2];
    [webView stringByEvaluatingJavaScriptFromString:setTextSizeRule];}
    

    【讨论】:

    • 从哪里可以得到“currentTextSize”?
    • 100 返回实际 html 字体大小。
    • 谢谢,我遇到了类似的问题,这让我走上了正轨,我只是将 -webkit-text-size-adjust 设置为 none。
    【解决方案3】:
        <meta name=\"viewport\" content=\"initial-scale=5.0\" /> 
    

    将此添加到 html 字符串中 为我工作 根据需要更改初始比例

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-03-26
      • 1970-01-01
      • 2012-05-07
      • 1970-01-01
      • 2011-11-07
      • 2023-03-11
      • 2023-03-07
      • 2021-06-13
      相关资源
      最近更新 更多