标签:ios uiwebview javascript 字体大小调整 objective-c
IOS UIWenView 调整页面字体大小,示例如下:
#import <UIKit/UIKit.h> @interface FontSizeChangeViewController : UIViewController{ UIToolbar *toolBar; } @property (weak, nonatomic) IBOutlet UIWebView *webDetails; @end
#import "FontSizeChangeViewController.h" #define K_FONT_SIZE_CHANGE_WEB_URL @"http://3g.fx678.com/news/detail/201508031037021902" #define K_FONT_SIZE_CHANGE_KEY @"ONT_SIZE_CHANGE_VIEW_SIZE" @interface FontSizeChangeViewController () @end @implementation FontSizeChangeViewController - (void)viewDidLoad { [super viewDidLoad]; [self initView]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } #pragma mark - Custom method - (void) initView{ //[S]加载网页 NSURL *url = [[NSURL alloc] initWithString:K_FONT_SIZE_CHANGE_WEB_URL]; // 自适应网页大小,支持放大缩小 self.webDetails.scalesPageToFit = YES; [self.webDetails loadRequest:[NSURLRequest requestWithURL:url]]; //[E]加载网页 //[S]设置toolbar CGSize cgsize = self.navigationController.toolbar.frame.size; if (!toolBar) toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height- cgsize.height, cgsize.width, cgsize.height)]; //字体放大按钮 UIBarButtonItem *barBtnAdd = [[UIBarButtonItem alloc] initWithTitle:@"字号+" style:UIBarButtonItemStylePlain target:self action:@selector(btnFontSizeChange:)]; barBtnAdd.tag = 1; //字体缩小按钮 UIBarButtonItem *barBtnSmall = [[UIBarButtonItem alloc] initWithTitle:@"字号-" style:UIBarButtonItemStylePlain target:self action:@selector(btnFontSizeChange:)]; barBtnAdd.tag = 2; NSArray *arrBtns = [NSArray arrayWithObjects:barBtnAdd,barBtnSmall, nil]; [toolBar setBarStyle:UIBarStyleDefault]; toolBar.autoresizingMask = UIViewAutoresizingFlexibleTopMargin; [toolBar setItems:arrBtns animated:YES]; [self.view addSubview:toolBar]; //[E]设置toolbar } //字体大小改变 -(void) btnFontSizeChange:(UIBarButtonItem *)sender{ NSInteger fontSize = [[NSUserDefaults standardUserDefaults] integerForKey:K_FONT_SIZE_CHANGE_KEY]; if (!fontSize){ //默认18px [[NSUserDefaults standardUserDefaults] setInteger:18 forKey:K_FONT_SIZE_CHANGE_KEY]; fontSize = [[NSUserDefaults standardUserDefaults] integerForKey:K_FONT_SIZE_CHANGE_KEY]; } NSString *strFontSize = nil; if (sender && [sender.title isEqualToString:@"字号+"]) fontSize += 2; else if(sender && [sender.title isEqualToString:@"字号-"]) fontSize -= 2; strFontSize = [NSString stringWithFormat:@"document.body.style.fontSize = '%lDpx';",(long)fontSize]; [self.webDetails stringByEvaluatingJavaScriptFromString:strFontSize]; //更新值 [[NSUserDefaults standardUserDefaults] setInteger:fontSize forKey:K_FONT_SIZE_CHANGE_KEY]; } @end
版权声明:本文为博主原创文章,未经博主允许不得转载。
标签:ios uiwebview javascript 字体大小调整 objective-c
原文地址:http://blog.csdn.net/yimiyuangguang/article/details/47260375