2011-01-24 100 views
0

你好 我想通过具有UIWebView子视图相同的UIViewController显示两个不同的HTML页面。UIWebView增加字体大小,如果有更多的文字

这两个html页面都使用相同的CSS并且结构相似。但是,我注意到,在iOS模拟器和设备上查看时,内容较少的页面的字体大小要比内容较多的页面的字体大小要小得多。

有人可以向我解释我能做些什么来在两个视图上具有相同的字体大小?

这里是我的UIWebView代码:

CGRect appFrame = [[UIScreen mainScreen] applicationFrame]; 
self.webView = [[[UIWebView alloc] initWithFrame: appFrame] autorelease]; 
self.webView.backgroundColor= [UIColor whiteColor]; 
self.webView.scalesPageToFit= YES; 
self.webView.autoresizingMask = (UIViewAutoresizingFlexibleWidth |  UIViewAutoresizingFlexibleHeight); 

NSString *filePath = [[NSBundle mainBundle] pathForResource:self.resourceName ofType:@"html"]; 
NSURL *urlLocation= [NSURL fileURLWithPath:filePath]; 
[self.webView loadRequest:[NSURLRequest requestWithURL:urlLocation]]; 
self.view = self.webView; 

下面是相关的HTML:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4 /loose.dtd"> 
<html> 
<head> 
<LINK href="manual.css" rel="stylesheet" type="text/css"> 
<title>My Info text</title> 
</head> 
<body> 
    <table > 
     <tr> 
     <td class="title"> 
       <b>How to do it</b> 
     </td> 
     </tr> 
     <tr> 
     <td class ="content"> 
      Some Instructions on how to do it properly. 
     </td> 
     </tr> 
    </table> 
</body> 
</html> 

而这里的CSS。主要的问题似乎是td.content,因为标题是尺寸不正确(或至少没有明显不同的两个屏幕之间):

body { 
background-color : rgb(255,255,255); 
font-size : 35px; 
font-family : Helvetica; 
color : rgb(54,54,54); 
margin-right : 0px; 
margin-left : 0px; 
} 

table { 
width : 100%; 
height: 100%; 
margin-top : 20px; 
margin-bottom : 20px; 
margin-right : 0px; 
margin-left : 0px; 
border-spacing : 0px; 
padding-right : 0px; 
padding-left : 0px; 
} 

tr { 
margin-top : 0px; 
margin-bottom : 0px;  
margin-right : 0px; 
margin-left : 0px; 
} 

td.content { 
font-size : 1em; 
text-align : left; 
vertical-align: top; 
margin-top : 0px; 
margin-bottom : 0px;  
margin-right : 0px; 
margin-left : 0px; 
padding-top : 5px; 
padding-bottom : 10px; 
padding-right : 30px; 
padding-left : 30px; 
} 

td.title { 
width : 170px; 
font-size : 1.5em; 
font-weight : bold; 
text-align : left; 
vertical-align : top; 
margin-top : 0px; 
margin-bottom : 0px;  
margin-right : 0px; 
margin-left : 0px; 
padding-top : 5px; 
padding-bottom : 10px; 
padding-right : 30px; 
padding-left : 30px; 
} 

回答

2

尝试设置到scalesPageToFit财产?

[myWebView setScalesPageToFit:NO]; 

您已将其设置为YES,这可能会影响它如何决定放大页面?

(从the docs here截取)

+0

如果我将其设置为NO,这两个视图显示了巨大的字体大小和文本超出屏幕远延伸这不是我想要的。 – Katlu 2011-01-24 15:43:35

+0

因此,在你的CSS中设置身体的宽度为320px,并使字体更小:)你的字体设置为35px - 这是相当大的 - 它只是做你告诉它! – deanWombourne 2011-01-24 16:45:19

0

我有2个按钮 - A-和A +

@interface 
NSUInteger textFontSize; 

- (IBAction)changeTextFontSize:(id)sender 

    switch ([sender tag]) { 
     case 1: // A- 
      textFontSize = (textFontSize > 50) ? textFontSize -5 : textFontSize; 
      break; 
     case 2: // A+ 
      textFontSize = (textFontSize < 160) ? textFontSize +5 : textFontSize; 
      break; 
    } 

    NSString *jsString = [[NSString alloc] initWithFormat:@"document.getElementsByTagName('body')[0].style.webkitTextSizeAdjust= '%d%%'", 
          textFontSize]; 
    [web stringByEvaluatingJavaScriptFromString:jsString]; 
    [jsString release]; 
}