【问题标题】:How to change the font size of the tab bar controller item name?如何更改标签栏控制器项目名称的字体大小?
【发布时间】:2012-06-27 07:29:07
【问题描述】:

我正在做一个基于 tabbarController 的应用程序。我有 3 个标签栏项目。

我的问题是:如何更改标签栏项目标题的字体样式?

【问题讨论】:

  • 相信我没有用户阅读文本,我宁愿花时间在一个非常好的描述性图标上
  • 是的。我希望它通过编程方式
  • 您能解释一下您的问题吗?您什么时候想更改标签栏项目的名称?是否有任何操作会调用方法来更改该选项卡栏的名称?还有你尝试过什么?
  • 就像当我运行应用程序时,所有的标签栏项目都会出现?到时候我所有带标题的图片都应该来了..
  • @doNotCheckMyBlog - 我强烈反对。 一旦读者知道选择的含义,图标就很有用,因为它们更容易一目了然。读者第一次看到选项时,文本更清晰。根据我的经验,大多数图标可能意味着任何可能的事物,文本阐明了这些事物。还要考虑到用户范围很广:有些人像你一样喜欢图标,有些人像我一样:我更愿意总是有(一个很小的、很短的)带有每个图标的文本,除了广泛标准化的图标。

标签: iphone objective-c xcode


【解决方案1】:
[self.tabBarItem setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                            [UIFont fontWithName:@"AmericanTypewriter" size:20.0f], UITextAttributeFont,
                                            [UIColor blackColor], UITextAttributeTextColor,
                                            [UIColor grayColor], UITextAttributeTextShadowColor,
                                            [NSValue valueWithUIOffset:UIOffsetMake(0.0f, 1.0f)], UITextAttributeTextShadowOffset,
                                            nil]];

【讨论】:

  • 它没有来......它显示警告并从那里终止
  • 当没有 UITabBarItem 的这种方法时,为什么这被投票了 7 次?
  • 对于任何对此仍有疑问的人:上面键入的方法不正确。您需要在setTitleTextAttributes: 之后添加forState: 参数。然后它将在没有警告的情况下编译并按预期工作。
【解决方案2】:

这将在整个应用程序中一劳永逸地更改您的 UITabBarItem 字体

对于 Swift,在 AppDelegate 的 didFinishLaunching 中使用它:

斯威夫特 3:

UITabBarItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName:UIColor.blue,NSFontAttributeName: UIFont(name: "Montserrat", size: 11)!], for: .normal)

UITabBarItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName:UIColor.red,NSFontAttributeName: UIFont(name: "Montserrat", size: 11)!], for: .selected)

【讨论】:

    【解决方案3】:

    抱歉,我认为没有办法做到这一点。如果你很绝望,你需要编写自己的标签栏。

    【讨论】:

    • 为什么要投票?针对这个较早的问题提出了解决方案:stackoverflow.com/q/2576592/199364。还是说这里的问题不一样?
    • @ToolmakerSteve 绝对正确。检查我的答案。
    【解决方案4】:

    遗憾的是,目前这在 iOS 上是不可能的,除非您构建自己的自定义标签栏,这对于 iOS5 上的故事板来说并不是很困难。

    【讨论】:

    • 抱歉,有可能。
    【解决方案5】:

    不可能,创建自定义标签栏子类化 UITabbar

    【讨论】:

    • 抱歉,有可能。
    【解决方案6】:

    如果你看到这个错误:'UITextAttributeTextShadowOffset' is deprecated: first deprecated in iOS 7.0 - Use NSShadowAttributeName with an NSShadow instance as the value.,试试这个。

    NSShadow *shadow = [[NSShadow alloc] init];
    shadow.shadowColor = [UIColor grayColor];
    shadow.shadowOffset = CGSizeMake(0.0, 0.5);
    
    NSDictionary *attribute = [NSDictionary dictionaryWithObjectsAndKeys:
    [UIFont fontWithName:@"AmericanTypewriter" size:10.0f], NSFontAttributeName,
    [UIColor blackColor], NSForegroundColorAttributeName,
     shadow,NSShadowAttributeName,nil];
    [[UITabBarItem appearance] setTitleTextAttributes:attribute forState:UIControlStateNormal];
    

    【讨论】:

      【解决方案7】:

      试试这个。

      [[UITabBarItem appearanceWhenContainedIn:[UITabBar class], nil]
        setTitleTextAttributes:@{NSForegroundColorAttributeName:
          [UIColor colorWithRed:0/255.0f green:130/255.0f blue:202/255.0f alpha:1.0],
          NSFontAttributeName:[UIFont fontWithName:@"Signika-Semibold" size:20.0]
        }
      forState:UIControlStateNormal];
      

      【讨论】:

      • 想弄清楚它到底做了什么?
      猜你喜欢
      • 2020-07-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-24
      • 1970-01-01
      • 2011-08-26
      • 1970-01-01
      • 2011-09-25
      相关资源
      最近更新 更多