我使用下面的代码来修复字体大小和文本对齐问题。希望它会对你有所帮助。
- (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