objective-c 如何在 UIWebView 中增加字体大小
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/589177/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to increase font size in UIWebView
提问by Bala
how to increase or decrease the UIWebview font size, not using scalePageToFit:NO;
如何增加或减少 UIWebview 字体大小,不使用 scalePageToFit:NO;
回答by Taras Kalapun
I have 2 buttons - A- and A+
我有 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];
}
回答by Girish Chauhan
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
NSString *fontSize=@"143";
NSString *jsString = [[NSString alloc] initWithFormat:@"document.getElementsByTagName('body')[0].style.webkitTextSizeAdjust= '%d%%'",[fontSize intValue]];
[myWebview stringByEvaluatingJavaScriptFromString:jsString];
}
回答by Louis Gerbarg
There is currently no exposed way to directly manipulate the DOM in a UIWebView, nor any convenience methods for handling things like font sizes. I suggest filing Radars.
目前没有直接操作 UIWebView 中的 DOM 的公开方法,也没有任何方便的方法来处理诸如字体大小之类的事情。我建议提交雷达。
Having said that. you can modify the font size by changing the CSS, like any other webpage. If that is not possible (you don't control the content) you can write a small javascript function to change the CSS properties in the pages DOM, and execute it by calling:
话说回来。您可以通过更改 CSS 来修改字体大小,就像任何其他网页一样。如果这是不可能的(您不控制内容),您可以编写一个小的 javascript 函数来更改页面 DOM 中的 CSS 属性,并通过调用执行它:
- (NSString *)stringByEvaluatingJavaScriptFromString:(NSString *)script;
回答by BLC
Swift 3
斯威夫特 3
public func webViewDidFinishLoad(_ webView: UIWebView) {
let textSize: Int = 300
webView.stringByEvaluatingJavaScript(from: "document.getElementsByTagName('body')[0].style.webkitTextSizeAdjust= '\(textSize)%%'")
}
Older Swift:
老斯威夫特:
func webViewDidFinishLoad(webView: UIWebView) {
let textSize: Int = 300
webView.stringByEvaluatingJavaScriptFromString("document.getElementsByTagName('body')[0].style.webkitTextSizeAdjust= '\(textSize)%%'")
}
回答by Hyman
In SWIFT 4 -
在 SWIFT 4 -
let fontSize = 20
let fontSetting = "<span style=\"font-size: \(fontSize)\"</span>"
webView.loadHTMLString( fontSetting + myHTMLString, baseURL: nil)
回答by Doolali
Swift 5 Update.This is an updated answer from @BLCs suggestion.
斯威夫特 5 更新。这是@BLCs 建议的更新答案。
Also fixes the double %% in his string.
还修复了他的字符串中的双%%。
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
let textSize = 300
let javascript = "document.getElementsByTagName('body')[0].style.webkitTextSizeAdjust= '\(textSize)%'"
webView.evaluateJavaScript(javascript) { (response, error) in
print()
}
}
回答by johnny
in my case im using a UISlider for the fontSize, can be any float value
在我的情况下,我使用 UISlider 作为 fontSize,可以是任何浮点值
func webViewDidFinishLoad(_ webView: UIWebView) {
if let fontSize: Float = (self.fontSizeSlider?.value) {
webView.stringByEvaluatingJavaScript(from: "document.getElementsByTagName('body')[0].style.webkitTextSizeAdjust= '\(fontSize)%%'")
print(fontSize)
}
}
回答by DavidNg
Here is mine for changing font size and color in UIWebView:
这是我在 UIWebView 中更改字体大小和颜色的方法:
NSString *myString=@"Hello World!";
int textFontSize=2;
NSString *myHTML=[NSString stringWithFormat: @"<html><body><script>var str = '%@'; document.write(str.fontsize(%d).fontcolor('green'));</script></body></html>",myString,textFontSize];
[myWebView loadHTMLString:myHTML baseURL:nil];
回答by Rajesh Loganathan
Try this simple method
试试这个简单的方法
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
int fontValue = 150;
NSString *webviewFontSize = [NSString stringWithFormat:@"addCSSRule('body', '-webkit-text-size-adjust: %d%%;')",fontValue];
[your_webview stringByEvaluatingJavaScriptFromString:webviewFontSize];
}
回答by Hiren
Try the below code :
试试下面的代码:
NSString *setTextSizeRule = [NSString stringWithFormat:@"addCSSRule('body', '-webkit-text-size-adjust: %d%%;')",currentTextSize];
[_webview stringByEvaluatingJavaScriptFromString:setTextSizeRule];

