通过按钮更改html字体大小

时间:2014-08-27 16:29:40

标签: html ios

大家好我写了一个简单的应用程序,在webview中显示一个html文件,html文件在主包中。 我想知道如何通过两个按钮使html文件字体大小更大/更小 这是我的.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

这是我的主要包中的html文件

<!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>

1 个答案:

答案 0 :(得分:0)

UIWebview有一个方法可以使用stringByEvaluatingJavaScriptFromString在当前站点的DOM内执行JavaScript命令。

使用以下方法调用简单的JavaScript命令,例如:

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

注意:您需要了解基本的JavaScript和HTML,以了解该命令在与Web内容交互时的工作方式。