0
我只在肖像模式下創建了一個基於頁面視圖控制器的應用程序,其中我在UIWebView中加載了多個html頁面。在Web視圖內部可以放大和縮小功能。當我點擊縮放按鈕時,只有當前頁面被縮放。我希望所有的Web視圖文本字體都是相同的。我正在嘗試但沒有正確輸出。如何在UIPageViewController中爲所有webViews文本字體啓用縮放功能
這裏是我的代碼....
- (void)viewDidLoad
{
[super viewDidLoad];
zoomButtonPressed = NO;
buttonPressed = NO;
//Instantiate the model array
self.htmlArray = [[NSMutableArray alloc] init];
NSString *path;
NSBundle *bundle = [NSBundle mainBundle];
for (int index = 1; index <= 94 ; index++)
{
path = [bundle pathForResource:[NSString stringWithFormat:@"p_%d",index]
ofType:@"html"];
[self.htmlArray addObject:path];
}
//Step 1
//Instantiate the UIPageViewController.
self.pageViewController = [[UIPageViewController alloc]
initWithTransitionStyle:UIPageViewControllerTransitionStylePageCurl
navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal
options:nil];
//Step 2:
//Assign the delegate and datasource as self.
self.pageViewController.delegate = self;
self.pageViewController.dataSource = self;
//Step 3:
//Set the initial view controllers.
ContentViewController *contentViewController = [[ContentViewController alloc]
initWithNibName:@"ContentViewController_iPad" bundle:nil];
contentViewController.strURL = [self.htmlArray objectAtIndex:0];
NSArray *viewControllers = [NSArray arrayWithObject:contentViewController];
[self.pageViewController setViewControllers:viewControllers
direction:UIPageViewControllerNavigationDirectionForward animated:NO
completion:nil];
//Step 4:
//Add the pageViewController as the childViewController
[self addChildViewController:self.pageViewController];
//Add the view of the pageViewController to the current view
[self.view addSubview:self.pageViewController.view];
[self.pageViewController didMoveToParentViewController:self];
//Step 5:
// set the pageViewController's frame as an inset rect.
CGRect pageViewRect;
pageViewRect = CGRectMake(5, 201, 745, 834);
pageViewRect = CGRectInset(pageViewRect, 12.0, 73.0);
self.pageViewController.view.frame = pageViewRect;
//Step 6:
self.view.gestureRecognizers = self.pageViewController.gestureRecognizers;
}
#pragma mark - UIPageViewControllerDataSource Methods
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController
viewControllerBeforeViewController:(UIViewController *)viewController
{
NSUInteger currentIndex = [self.htmlArray indexOfObject:[(ContentViewController
*)viewController strURL]];
if(currentIndex == 0)
{
return nil;
}
ContentViewController *contentViewController = [[ContentViewController alloc]
init];
contentViewController.strURL = [self.htmlArray objectAtIndex:currentIndex - 1];
buttonPressed = NO;
if (currentIndex % 2 == 0) {
content=contentViewController;
}
else {
content1=contentViewController;
}
if (zoomButtonPressed)
{
[self setTextZooming];
}
return contentViewController;
}
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController
viewControllerAfterViewController:(UIViewController *)viewController
{
NSUInteger currentIndex = [self.htmlArray indexOfObject:[(ContentViewController
*)viewController strURL]];
if(currentIndex == self.htmlArray.count-1)
{
return nil;
}
ContentViewController *contentViewController = [[ContentViewController alloc]
init];
contentViewController.strURL = [self.htmlArray objectAtIndex:currentIndex + 1];
buttonPressed = NO;
if (currentIndex % 2 == 0) {
content=contentViewController;
}
else {
content1=contentViewController;
}
if (zoomButtonPressed)
{
[self setTextZooming];
}
return contentViewController;
}
#pragma mark - UIPageViewControllerDelegate Methods
- (UIPageViewControllerSpineLocation)pageViewController:(UIPageViewController
*)pageViewController
spineLocationForInterfaceOrientation:(UIInterfaceOrientation)orientation
{
if(UIInterfaceOrientationIsPortrait(orientation))
{
//Set the array with only 1 view controller
UIViewController *currentViewController = [self.pageViewController.viewControllers
objectAtIndex:0];
NSArray *viewControllers = [NSArray arrayWithObject:currentViewController];
[self.pageViewController setViewControllers:viewControllers
direction:UIPageViewControllerNavigationDirectionForward animated:YES
completion:NULL];
//Important- Set the doubleSided property to NO.
self.pageViewController.doubleSided = NO;
//Return the spine location
return UIPageViewControllerSpineLocationMin;
}
else
{
NSArray *viewControllers = nil;
ContentViewController *currentViewController =
[self.pageViewController.viewControllers objectAtIndex:0];
NSUInteger currentIndex = [self.htmlArray indexOfObject:[(ContentViewController
*)currentViewController strURL]];
if(currentIndex == 0 || currentIndex %2 == 0)
{
UIViewController *nextViewController = [self
pageViewController:self.pageViewController
viewControllerAfterViewController:currentViewController];
viewControllers = [NSArray arrayWithObjects:currentViewController,
nextViewController, nil];
}
else
{
UIViewController *previousViewController = [self
pageViewController:self.pageViewController
viewControllerBeforeViewController:currentViewController];
viewControllers = [NSArray arrayWithObjects:previousViewController,
currentViewController, nil];
}
//Now, set the viewControllers property of UIPageViewController
[self.pageViewController setViewControllers:viewControllers
direction:UIPageViewControllerNavigationDirectionForward animated:YES
completion:NULL];
return UIPageViewControllerSpineLocationMid;
}
}
-(void) setTextZooming
{
if (!buttonPressed){
content.webView.scrollView.scrollEnabled = YES;
content1.webView.scrollView.scrollEnabled = YES;
int fontSize = 120;
NSString *string = [[NSString alloc]
initWithFormat:@"document.getElementsByTagName('body')
[0].style.webkitTextSizeAdjust= '%d%%'", fontSize];
[content.webView stringByEvaluatingJavaScriptFromString:string];
[content1.webView stringByEvaluatingJavaScriptFromString:string];
buttonPressed=YES;
zoomButtonPressed = YES;
}
else{
content.webView.scrollView.scrollEnabled = NO;
content1.webView.scrollView.scrollEnabled = NO;
int fontSize = 97;
NSString *string = [[NSString alloc]
initWithFormat:@"document.getElementsByTagName('body')
[0].style.webkitTextSizeAdjust= '%d%%'", fontSize];
[content.webView stringByEvaluatingJavaScriptFromString:string];
[content1.webView stringByEvaluatingJavaScriptFromString:string];
buttonPressed=NO;
zoomButtonPressed = NO;
}
}
// When user clicks on zoom button.
-(IBAction)zoomBtnPressed:(id)sender
{
[self setTextZooming];
}