简体   繁体   中英

change html font size by a button

hi guys i wrote a simple app that displays an html file in a webview ,the html file is in the main bundle. i was wondering how can i make the html file font-size bigger/smaller by a two buttons this is my .m

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController
@synthesize webView;

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    [webView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"jama" ofType:@"htm"]isDirectory:NO]]];


}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)increase:(id)sender {

    }

- (IBAction)decrease:(id)sender {

    }


@end

and this is my html file which is in my main bundle

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  <meta http-equiv="Content-Style-Type" content="text/css">
  <title></title>
  <meta name="Generator" content="Cocoa HTML Writer">
  <meta name="CocoaVersion" content="1265.21">
  <style type="text/css">
    p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; text-align: left; font: 21.0px 'Geeza Pro'}
  </style>
</head>
<body>
<p dir="rtl" class="p1"><b> hello </b></p>
</body>
</html>

UIWebview has a method that can execute JavaScript command inside the DOM of the current site using stringByEvaluatingJavaScriptFromString .

With the following method call a simple JavaScript command such as:

// your javascript command that will alter the layout og your html 
NSString *javascript = @"document.getElementById('myP').style.fontSize='large';";

// tell the webview to execute a script on your current page. this method must be called once the page has completely loaded.  
[webView stringByEvaluatingJavaScriptFromString:javascript];

Note: you need to know basic JavaScript and HTML to understand how the command works as your interacting with with web content.

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