how to increase font size in UIWebView

前端 未结 11 1013
广开言路
广开言路 2020-11-30 18:12

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

相关标签:
11条回答
  • 2020-11-30 18:42

    In SWIFT -

    let fontSize = 20
    let fontSetting = "<span style=\"font-size: \(fontSize)\"</span>"
    webView.loadHTMLString( fontSetting + myHTMLString, baseURL: nil)
    
    0 讨论(0)
  • 2020-11-30 18:42

    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.

    0 讨论(0)
  • 2020-11-30 18:46

    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)%%'")
    }
    
    0 讨论(0)
  • 2020-11-30 18:46

    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];
    }
    
    0 讨论(0)
  • 2020-11-30 18:50
    - (void)webViewDidFinishLoad:(UIWebView *)webView
    {
        NSString *fontSize=@"143";
        NSString *jsString = [[NSString alloc]      initWithFormat:@"document.getElementsByTagName('body')[0].style.webkitTextSizeAdjust= '%d%%'",[fontSize intValue]];
        [myWebview stringByEvaluatingJavaScriptFromString:jsString];
    
    }
    
    0 讨论(0)
提交回复
热议问题