简体   繁体   English

如何在 UIWebView 中增加字体大小

[英]how to increase font size in UIWebView

how to increase or decrease the UIWebview font size, not using scalePageToFit:NO;如何增加或减少 UIWebview 字体大小,不使用 scalePageToFit:NO;

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];
}
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    NSString *fontSize=@"143";
    NSString *jsString = [[NSString alloc]      initWithFormat:@"document.getElementsByTagName('body')[0].style.webkitTextSizeAdjust= '%d%%'",[fontSize intValue]];
    [myWebview stringByEvaluatingJavaScriptFromString:jsString];

}

There is currently no exposed way to directly manipulate the DOM in a UIWebView, nor any convenience methods for handling things like font sizes.目前没有公开的方法来直接操作 UIWebView 中的 DOM,也没有任何方便的方法来处理诸如字体大小之类的事情。 I suggest filing Radars .我建议归档Radars

Having said that.话说回来。 you can modify the font size by changing the CSS, like any other webpage.您可以通过更改 CSS 来修改字体大小,就像任何其他网页一样。 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:如果这是不可能的(你不控制内容)你可以写一个小的 javascript function 来改变 CSS 在页面 DOM 中的属性,并通过调用:

- (NSString *)stringByEvaluatingJavaScriptFromString:(NSString *)script;

Swift 3 Swift 3

public func webViewDidFinishLoad(_ webView: UIWebView) {        
    let textSize: Int = 300
    webView.stringByEvaluatingJavaScript(from: "document.getElementsByTagName('body')[0].style.webkitTextSizeAdjust= '\(textSize)%%'")
}

Older Swift :旧版 Swift

func webViewDidFinishLoad(webView: UIWebView) {
    let textSize: Int = 300

    webView.stringByEvaluatingJavaScriptFromString("document.getElementsByTagName('body')[0].style.webkitTextSizeAdjust= '\(textSize)%%'")
}

Try the below code:试试下面的代码:

NSString *setTextSizeRule = [NSString stringWithFormat:@"addCSSRule('body', '-webkit-text-size-adjust: %d%%;')",currentTextSize];
[_webview stringByEvaluatingJavaScriptFromString:setTextSizeRule];

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];

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

in my case im using a UISlider for the fontSize, can be any float value在我的情况下,我使用 UISlider 作为字体大小,可以是任何浮点值

func webViewDidFinishLoad(_ webView: UIWebView) {

    if let fontSize: Float = (self.fontSizeSlider?.value) {
        webView.stringByEvaluatingJavaScript(from: "document.getElementsByTagName('body')[0].style.webkitTextSizeAdjust= '\(fontSize)%%'")
        print(fontSize)
    }

}

In SWIFT -在 SWIFT -

let fontSize = 20
let fontSetting = "<span style=\"font-size: \(fontSize)\"</span>"
webView.loadHTMLString( fontSetting + myHTMLString, baseURL: nil)

Swift 5 Update. Swift 5 更新。 This is an updated answer from @BLCs suggestion.这是来自@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()
    }
}

Change the font size of the fetched HTML data by changing the occurrence of font size in the fetched html string to your required font size (40 in this example) in a method.通过在方法中将获取的 html 字符串中出现的字体大小更改为所需的字体大小(本示例中为 40),更改获取的 HTML 数据的字体大小。

strHtml = [self htmlEntityDecode:strHtml];//parsing the html string


-(NSString *)htmlEntityDecode:(NSString *)string
{    
string = [string stringByReplacingOccurrencesOfString:@"14px;"   withString:@"40"];
string = [string stringByReplacingOccurrencesOfString:@"15px;"   withString:@"40"];
string = [string stringByReplacingOccurrencesOfString:@"16px;"   withString:@"40"];
string = [string stringByReplacingOccurrencesOfString:@"17px;"   withString:@"40"];
string = [string stringByReplacingOccurrencesOfString:@"18px;"   withString:@"40"];
string = [string stringByReplacingOccurrencesOfString:@"19px;"   withString:@"40"];
string = [string stringByReplacingOccurrencesOfString:@"20px;"   withString:@"40"];

 return string;
}

So the html string: span style='font-size: 20px;所以 html 字符串:span style='font-size: 20px; Becomes: span style='font-size: 40变为:span style='font-size: 40

Note: Change the other occurrences like gt;, apos;注意:更改其他匹配项,例如 gt;、apos; too to get the desired string to load in your Webview.也可以将所需的字符串加载到您的 Webview 中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM