我使用下面的代碼來修復字體大小和文本對齊問題。希望它會對你有所幫助。
- (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];}
當我們縮放肯定會影響大小 改變初始規模,但是當我不縮放頁面,然後它也顯示不同的字體大小。這是一個書籍應用程序,所以我使用了UIPageViewController,我在其中顯示UIWebView,頁面寬度相同。 – Reena 2013-05-11 06:53:05
@Reena我知道web視圖可能是相同的,但由HTML確定的頁面寬度可能不同。 UIWebView將嘗試適應整個HTML頁面,所以如果你在一個頁面上獲得了一些寬泛的內容,而另一個頁面上卻沒有寬泛的內容,那麼頁面上包含更多內容的文本會變得更小。 – Caleb 2013-05-11 06:58:25
同意。我會檢查 – Reena 2013-05-11 07:15:22