how to increase font size in UIWebView

2019-01-08 04:21发布

how to increase or decrease the UIWebview font size, not using scalePageToFit:NO;

9条回答
贼婆χ
2楼-- · 2019-01-08 04:36

Here is mine for changing font size and color in 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];
查看更多
男人必须洒脱
3楼-- · 2019-01-08 04:37

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];
}
查看更多
在下西门庆
4楼-- · 2019-01-08 04:43

Swift 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)%%'")
}
查看更多
贼婆χ
5楼-- · 2019-01-08 04:43

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.

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; Becomes: span style='font-size: 40

Note: Change the other occurrences like gt;, apos; too to get the desired string to load in your Webview.

查看更多
贪生不怕死
6楼-- · 2019-01-08 04:46

in my case im using a UISlider for the fontSize, can be any float value

func webViewDidFinishLoad(_ webView: UIWebView) {

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

}
查看更多
神经病院院长
7楼-- · 2019-01-08 04:51

I have 2 buttons - A- and 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];
}
查看更多
登录 后发表回答