简体   繁体   中英

how to resize the wkwebview font size?

How to change the font size of WKWebView irrespective of the webkit content in iOS programming?

I am using [Webview stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"resizeText(%lu)",(unsigned long)fontSize]];

But it doesn't make any changes to the web view font size.

I suggest you add CSS code to the content.

I had to do this one time, and here's an example from my code:

NSString *systemFont = [[[UIFont systemFontOfSize:11.f] description] fontCSS];
NSString *boldFont = [[[UIFont boldSystemFontOfSize:12.f] description] fontCSS];
// The above fonts can be changed to your liking.
NSString *oldContent = ___; // This is the html content that was loaded.
NSString *content = [NSString stringWithFormat:@"<html><head>\
                   <style>\
                   img,iframe{max-width:100%%; width:auto; height:auto; display:block; margin-left:auto; margin-right:auto; margin-bottom:20px}\
                   body{%@; margin-left:24px; margin-right:24px; margin-bottom:24px}\
                   strong{%@}\
                   </style>\
                   </head><body>%@</body></html>", systemFont, boldFont, oldContent];

Now you use the content variable with your webview .

The fontCSS method is as follows, it's in an NSString category:

-(NSString *)fontCSS
{
    NSRange range = [self rangeOfString:@"font-family"];

    return [self substringFromIndex:range.location];
} 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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