如何在UIPageViewController中为所有webViews文本字体启用缩放

时间:2013-07-08 09:49:13

标签: html iphone ios uiwebview uipageviewcontroller

我只在纵向模式下创建一个基于页面视图控制器的应用程序,我在UIWebView中加载多个html页面。 Web视图内部具有放大和缩小功能。当我单击缩放按钮时,只有当前页面被缩放。我希望所有的Web视图文本字体都相同。我正在尝试但没有正确输出。

这是我的代码......

   - (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];
  }

1 个答案:

答案 0 :(得分:0)

我发现了问题:

在UIPageViewController委托方法

      - (UIViewController *)pageViewController:(UIPageViewController *)pageViewController
        viewControllerAfterViewController:(UIViewController *)viewController

      - (UIViewController *)pageViewController:(UIPageViewController *)pageViewController
        viewControllerBeforeViewController:(UIViewController *)viewController 

问题是在返回我的视图控制器之前我直接设置了字体增加方法。方法“setTextZooming”正在调用,但它将永远不会工作,因为正在进行事务(动画)。所以设置动画:否或使用此..

     if (zoomButtonPressed)
     {
         [self performSelector:@selector(setTextZooming) withObject:nil afterDelay:0.4];
     }

这对我有用......