2013-05-11 85 views
2

我在iPhone應用程序的一個UIWebView中顯示HTML內容。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> 

請幫助。

回答

0

UIWebView支持縮放,這自然會影響類型的大小。很有可能當你看到更大的類型時,你只是進一步放大,也許是因爲這兩個頁面的寬度不同。

+0

當我們縮放肯定會影響大小 改變初始規模,但是當我不縮放頁面,然後它也顯示不同的字體大小。這是一個書籍應用程序,所以我使用了UIPageViewController,我在其中顯示UIWebView,頁面寬度相同。 – Reena 2013-05-11 06:53:05

+0

@Reena我知道web視圖可能是相同的,但由HTML確定的頁面寬度可能不同。 UIWebView將嘗試適應整個HTML頁面,所以如果你在一個頁面上獲得了一些寬泛的內容,而另一個頁面上卻沒有寬泛的內容,那麼頁面上包含更多內容的文本會變得更小。 – Caleb 2013-05-11 06:58:25

+0

同意。我會檢查 – Reena 2013-05-11 07:15:22

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];} 
+0

從哪裏我可以得到「currentTextSize」? – Reena 2013-05-11 07:27:39

+0

100返回實際的html字體大小。 – Ashini 2013-05-11 07:32:31

+1

謝謝,我有一個類似的問題,這使我在正確的軌道上,我只是將-webkit文本大小調整爲無。 – Paludis 2013-11-20 02:01:51

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

已將此添加到HTML字符串 爲我工作的要求