IOS UIWenView 調整頁面字體大小,示例如下:
#import@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