【问题标题】:Is there any way to change the title font size in a UIActionSheet?有什么方法可以更改 UIActionSheet 中的标题字体大小?
【发布时间】:2011-04-24 14:36:10
【问题描述】:

字体非常小,有些人难以阅读,所以我想让它更接近下方按钮上使用的字体大小。

【问题讨论】:

  • 可能没有支持的方式来执行此操作。您可能会以某种方式获得对操作表视图的引用,然后遍历它以找到标题 UILabel 并直接编辑该视图,但如果操作表的操作系统实现发生更改,将来可能会中断。

标签: iphone font-size uiactionsheet


【解决方案1】:

就像 Nimrod 所说的,您不能使用本机方法执行此操作。您可以检查 UIActionSheet 子视图,找到好的子视图并更改其属性。

但是

  • 此实现可能会在 未来的 iOS 更新
  • Apple 可能会拒绝您的应用
  • UIActionSheet 是 iPhone GUI 和用户都熟悉 有了它

所以

你真的不应该改变使用的字体大小。如果这个 UIActionSheet 标题很重要,你应该找到另一种方式向用户展示它......

【讨论】:

    【解决方案2】:

    您可以在 show 方法后更改字体属性(如 showFromBarButtonItem),如下所示。

    CGRect oldFrame = [(UILabel*)[[sheet subviews] objectAtIndex:0] frame];
    UILabel *newTitle = [[[UILabel alloc] initWithFrame:oldFrame] autorelease];
    newTitle.font = [UIFont boldSystemFontOfSize:17];
    newTitle.textAlignment = UITextAlignmentCenter;
    newTitle.backgroundColor = [UIColor clearColor];
    newTitle.textColor = [UIColor whiteColor];
    newTitle.text = @"My Title";
    [sheet addSubview:newTitle];
    
    [sheet release];
    

    【讨论】:

    • 优秀的解决方案。而在show 方法之后这样做的原因是我们在show 之前没有动作表框架及其子视图
    • 很好的答案,有很好的解释!!
    【解决方案3】:

    嗯,有办法做到这一点 - 我在 github 上创建了一个 UIActionSheet 子类,名为 LTActionSheet

    正如其他人所提到的,如果您使用它,可能会发生各种可怕的事情(我还没有运输代码......但是:-))如果您在被接受的应用程序中使用它,请让我们都知道!

    【讨论】:

      【解决方案4】:

      @user651909 的回答对我来说非常有用,尽管只是为了添加更多细节:

      在创建UIActionSheet 时,我必须initWithTitle:@" "(注意非空字符串),以便视图顶部有一些空间可用于任何文本开头。然后,在做了[popupQuery showInView:self.view]; 之后,我添加了他的建议,以便初始化 oldFrame。最后,我补充说:

      [newTitle sizeToFit];
      newTitle.frame = CGRectMake(oldFrame.origin.x, 
                                  oldFrame.origin.y, 
                                  oldFrame.size.width, 
                                  newTitle.frame.size.height);
      

      这是必要的,因为oldFrame 的高度对于我的较大字体(20 号)来说太小了,导致一些字母在底部被剪掉。即使有这个调整,如果你让文本太大,比如大于boldSystemFontOfSize:26,文本会跑得太近或进入下面的按钮。调整工作表框架的大小并没有帮助,因为按钮似乎固定在顶部。

      大概在做

      CGRect oldFrame = [(UILabel*)[[sheet subviews] objectAtIndex:0] frame]; 
      

      不违反 Apple 不使用任何内部 API 的政策,对吧?

      【讨论】:

        【解决方案5】:
        UIActionSheet *as = [[UIActionSheet alloc] initWithTitle:@"SpecifySearch" 
                        delegate:self 
               cancelButtonTitle:@"Cancel" 
          destructiveButtonTitle:nil 
               otherButtonTitles:@"UncategorizedContacts" , nil];
        
        //as.actionSheetStyle = UIActionSheetStyleBlackTranslucent;
        [as showFromTabBar:self.tabBarController.tabBar];
        
        if( as.subviews.count > 0 
            && [[as.subviews objectAtIndex:0] isKindOfClass:[UILabel class]] ){
            UILabel* l = (UILabel*) [as.subviews objectAtIndex:0];
            [l setFont:[UIFont boldSystemFontOfSize:20]];
        }
        
        [as release];
        

        【讨论】:

          【解决方案6】:

          正如conecon 所展示的,您可以获得指向它的指针并对其进行操作。如果我理解正确你想要做的是:

          [sheet showInView:[self.view window]];
          UILabel *title = [[sheet subviews] objectAtIndex:0];
          title.font = [UIFont boldSystemFontOfSize:20];
          title.textAlignment = UITextAlignmentCenter;
          

          希望这会有所帮助!

          【讨论】:

            【解决方案7】:
            UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:@"Share" delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:@"Facebook", @"Twitter",nil];
            [sheet showInView:self.view];
            if (![[[UIDevice currentDevice] systemVersion] floatValue] >= 7) {
                 CGRect labelFrame = [(UILabel*)[[sheet subviews] objectAtIndex:0] frame];
                 [[[sheet subviews] objectAtIndex:0] removeFromSuperview];
                 UILabel *newTitle = [[UILabel alloc] initWithFrame:labelFrame];
                 newTitle.font = [UIFont boldSystemFontOfSize:17];
                 newTitle.textAlignment = UITextAlignmentCenter;
                 newTitle.backgroundColor = [UIColor clearColor];
                 newTitle.textColor = [UIColor whiteColor];
                 newTitle.text = @"Share";
                 [sheet addSubview:newTitle];
             }
            

            【讨论】:

              【解决方案8】:

              来自UIActionSheet Class Reference

              UIActionSheet 并非设计为子类化,也不应将视图添加到其层次结构中。如果您需要呈现比 UIActionSheet API 提供的自定义更多的表单,您可以创建自己的表单并使用presentViewController:animated:completion: 以模态方式呈现它。

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 2012-07-22
                • 2013-11-03
                • 1970-01-01
                • 2017-09-28
                • 1970-01-01
                • 2020-03-21
                • 2021-12-23
                • 1970-01-01
                相关资源
                最近更新 更多