UIWebView HTML字体大小问题

时间:2013-05-11 06:24:24

标签: ios uiwebview

我在我的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>

请帮忙。

3 个答案:

答案 0 :(得分:2)

    <meta name=\"viewport\" content=\"initial-scale=5.0\" /> 

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

答案 1 :(得分:1)

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

- (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];}

答案 2 :(得分:0)

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

相关问题