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

Posted

技术标签:

【中文标题】如何为 UIPageViewController 中的所有 webViews 文本字体启用缩放【英文标题】:How to enable zoom for all the webViews text font in UIPageViewController 【发布时间】:2013-07-08 09:49:12 【问题描述】:

我只在纵向模式下创建一个基于页面视图控制器的应用程序,我在 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】:

我发现了问题:

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

这对我有用...

【讨论】:

以上是关于如何为 UIPageViewController 中的所有 webViews 文本字体启用缩放的主要内容,如果未能解决你的问题,请参考以下文章

如何用 UIScrollView 实现 UIPageViewController?

UIPageViewController:如何处理视图控制器的不同方向?

如何处理 UIPageViewController 中的向后滚动?

UIPageViewController 与滚动视图奇数插入反弹

图像的简单横向滚动 - UIScrollView 或 UIPageViewController?

如何使用现有 UIPageViewController 的按钮以编程方式滑动视图