【问题标题】:Change font type and size of UIActionSheet title string更改 UIActionSheet 标题字符串的字体类型和大小
【发布时间】:2012-07-22 16:01:24
【问题描述】:

我有一个标题字符串为“DO:这些任务”的 UIActionSheet。在标题字符串中,子字符串“DO:”应该是粗体(具有特定的字体大小),子字符串“这些任务”应该是常规的。可能吗?我该怎么做?

【问题讨论】:

    标签: iphone ios uiactionsheet font-size


    【解决方案1】:

    我假设您有一个实现 UIActionSheetDelegate 协议的类,并且您的类是当前 UIActionSheet 的委托类,因此在该类中您可以更改 @ 的整个标题987654323@点赞

    - (void)willPresentActionSheet:(UIActionSheet *)actionSheet {
        for (UIView *_currentView in actionSheet.subviews) {
            if ([_currentView isKindOfClass:[UILabel class]]) {
                [((UILabel *)_currentView) setFont:[UIFont boldSystemFontOfSize:15.f]];
            }
        }
    }
    

    但据您所知,您没有机会格式化 UILabel 对象的 text 属性的一部分。


    您现在可以为您的actionsheet 添加一个新的UILabel,使用不同的字体,如果您想查看带有粗体DO: 字符串的文本...但是从这里开始,您只能想象,您可以在此方法中或在-didPresentActionSheet: 方法中格式化UIActionSheet 的元素。

    所以这就是这个想法

    2013 年 10 月 7 日更新

    在 iOS7 中,我们并不能直接访问一个 UIAlertView UIActionSheet 对象的子视图,所以这个解决方案可能不会在 iOS7 环境下可以正常工作。

    如果您在 iOS7 上遇到任何问题,请给我评论!谢谢。

    2014 年 6 月 22 日更新

    我还没有找到任何解决方案可以直接在 iOS8 中使用新的UIAlertController 执行此操作,标题标签在UIAlertController 的整个视图层次结构中似乎不可见,在它出现之前不可见,甚至不可见之后。

    注意:我还发现不禁止重叠/覆盖其内容,它出现在屏幕上之后。目前无法预测它是否会在 Beta 版上运行,或者它是否是 AppStore 安全的。

    注意:请牢记视图生命周期的时间表,如果导航堆栈中尚未包含任何内容,请不要尝试在视图或视图控制器中呈现任何内容。

    不管怎样,这里有一个 Swift 解决方案,我可以如何到达这些层,我可以将我的自定义视图添加到它。

    let alertController: UIAlertController = UIAlertController(title: "", message: "", preferredStyle: UIAlertControllerStyle.Alert)
    self.presentViewController(alertController, animated: true, completion: {
        let aboveBlurLayer: UIView = alertController.view.subviews[0] as UIView
        let belowBlurLayer: UIView = (aboveBlurLayer.subviews[0].subviews as Array<AnyObject>)[0] as UIView
        let customView: UIView = UIView(frame: aboveBlurLayer.bounds)
        customView.backgroundColor = UIColor.redColor()
        aboveBlurLayer.addSubview(customView)
        })
    

    注意:将自定义内容添加到警报/操作表可能会很有用,但如果将来它不起作用,我会更新我的答案。

    【讨论】:

      【解决方案2】:

      对于 iOS 7,以下代码可以使用。

      如下实现UIActionSheetDelegate

      - (void)willPresentActionSheet:(UIActionSheet *)actionSheet {
          [actionSheet.subviews enumerateObjectsUsingBlock:^(id _currentView, NSUInteger idx, BOOL *stop) {
              if ([_currentView isKindOfClass:[UIButton class]]) {
                  [((UIButton *)_currentView).titleLabel setFont:[UIFont boldSystemFontOfSize:15.f]];
                  // OR
                  //[((UIButton *)_currentView).titleLabel setFont:[UIFont fontWithName:@"Exo2-SemiBold" size:17]];
              }
          }];
      }
      

      适用于 iOS 6

      - (void)willPresentActionSheet:(UIActionSheet *)actionSheet {
          for (UIView *_currentView in actionSheet.subviews) {
              if ([_currentView isKindOfClass:[UILabel class]]) {
                  [(UILabel *)_currentView setFont:[UIFont boldSystemFontOfSize:15.f]];
                  // OR
                  //[(UILabel *)_currentView setFont:[UIFont fontWithName:@"Exo2-SemiBold" size:17]];
              }
          }
      }
      

      【讨论】:

      • 由于某种原因,这不会改变取消按钮的字体:(
      • 嘿@samvermette 上面已经过测试并在我的一个应用程序中使用:) 工作正常。如果您发现任何其他解决方案,请更新。
      • @icodebuster 你的解决方案对我来说在 iOS 7 上运行良好,谢谢。
      • 这似乎只能调整取消按钮。我不知道为什么其他按钮不改变......
      • @ColinTremblay 您使用的是哪个版本的 iOS?
      【解决方案3】:

      根据 Holex 的回答:

      对于 iOS7 和 iOS6,以下内容已帮助我更改 UIActionSheet 属性的标题。请注意,我实际上是在添加一个新的子视图,因为未知?为什么更新当前的 UILabel 只给出垂直一半的文本? “title”显然是第一个 UILabel,所以我们在一次更改后中断循环。

      - (void)willPresentActionSheet:(UIActionSheet *)actionSheet {
          for (UIView *_currentView in actionSheet.subviews) {
              if ([_currentView isKindOfClass:[UILabel class]]) {
                  UILabel *l = [[UILabel alloc] initWithFrame:_currentView.frame];
                  l.text = [(UILabel *)_currentView text];
                  [l setFont:[UIFont fontWithName:@"Arial-BoldMT" size:20]];
                  l.textColor = [UIColor darkGrayColor];
                  l.backgroundColor = [UIColor clearColor];
                  [l sizeToFit];
                  [l setCenter:CGPointMake(actionSheet.center.x, 25)];
                  [l setFrame:CGRectIntegral(l.frame)];
                  [actionSheet addSubview:l];
                  _currentView.hidden = YES;
                  break;
              }
          }
      }
      

      以上似乎适用于 iOS6 和 iOS7 模拟器。不过,我不确定这是否适用于其他未来版本的 iOS。

      【讨论】:

        【解决方案4】:

        以下可能有用。

        - (void)willPresentActionSheet:(UIActionSheet *)actionSheet
        {
            [actionSheet.subviews enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
                if ([obj isKindOfClass:[UIButton class]]) {
                    UIButton *button = (UIButton *)obj;
                    button.titleLabel.font = [UIFont systemFontOfSize:15];
                }
            }];
        
        }
        

        【讨论】:

          【解决方案5】:
          【解决方案6】:

          UIActionSheet 的内部属性称为_titleLabel,因此可以获取引用并直接更改此标签的属性字符串属性。

          请注意,这是一个私有 API,可能会在 App Store 中被拒绝。

          这是它的工作原理:

          - (NSAttributedString *) actionSheetAttributedTitle;
          {
              NSMutableAttributedString *attString=[[NSMutableAttributedString alloc] initWithString:@"Test"];
              UIFont *font=[UIFont fontWithName:@"Helvetica-Bold" size:13.0f];
              [attString addAttribute:NSFontAttributeName value:font range:NSMakeRange(0, attString.length)];
              [attString addAttribute:NSForegroundColorAttributeName
                            value:[UIColor blackColor]
                            range:NSMakeRange(0, attString.length)];
              return [attString copy];
          }
          
          - (void)willPresentActionSheet:(UIActionSheet *)actionSheet;
          {
              UILabel *sheetTitleLabel;
              if([actionSheet respondsToSelector:@selector(_titleLabel)]) {
                  sheetTitleLabel = objc_msgSend(actionSheet, @selector(_titleLabel));
                  sheetTitleLabel.attributedText = [self actionSheetAttributedTitle];
          
              }
          }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2017-12-23
            • 2011-04-24
            • 1970-01-01
            • 1970-01-01
            • 2019-02-05
            • 1970-01-01
            • 1970-01-01
            • 2011-08-26
            相关资源
            最近更新 更多