如何更改选项卡控制器项目名称的字体大小?

16

我正在开发一个基于tabbarController的应用。我有3个tabbar项目。

我的问题是:如何为选项卡项目上的标题更改字体样式?


1
相信我,用户都不会读这段文字,我更愿意花时间设计一个真正好的描述性图标。 - TeaCupApp
是的,我想通过编程方式进行翻译。 - Manu
你能解释一下你的问题吗?你想什么时候更改选项卡栏项目的名称?是否有任何操作可以调用更改选项卡栏名称的方法?还有你尝试过什么? - janusfidel
当我运行应用程序时,所有选项卡栏项目都会出现,是吗?此时,我的所有带标题的图像应该出现。 - Manu
1
@doNotCheckMyBlog - 我强烈不同意。一旦读者知道选择的含义,图标就很有用,因为它们更容易一眼看出来。读者第一次看到选择时,文本更清晰。根据我的经验,大多数图标可能意味着许多可能的事情,而文本可以澄清这些事情。还要考虑到有各种各样的用户:有些人像你一样喜欢图标,而另一些人像我一样:除了广泛标准化的图标外,我希望每个图标都有一个(微小的、简短的)文本。 - ToolmakerSteve
7个回答

12
[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]];

1
它没有出现.. 它显示警告并从那里终止。 - Manu
1
为什么这个问题会被点赞7次,UITabBarItem中并没有这样的方法? - devmiles.com
3
对于仍然遇到问题的人:上述方法是不正确的。您需要在setTitleTextAttributes:后添加forState:参数。然后,它将可以编译而且按预期工作。 - element119

2

这将在整个应用程序中一次性更改您的UITabBarItem字体。

对于Swift,请在AppDelegate的didFinishLaunching中使用以下代码:

Swift 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)

0

抱歉,我认为没有办法做到这一点。如果你非常需要,你需要编写自己的选项卡栏。


2
为什么这个帖子会被点赞?之前的这个问题已经有解决方案了:https://dev59.com/aHE85IYBdhLWcg3w2Hfs。还是你在说这里的问题不同? - ToolmakerSteve
@ToolmakerSteve 绝对正确。请查看我的答案。 - Ankit Kumar Gupta

0

很遗憾,目前在iOS上无法实现这一点,除非您构建自己的自定义选项卡栏,在iOS5上使用故事板并不是很困难。


抱歉,这是可能的。 - ABCD

0

无法实现,需要通过继承UITabbar创建自定义选项卡栏


抱歉,这是可能的。 - ABCD

0
如果您看到此错误:'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];

-1

试试这个。

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

能否澄清一下它确切的作用是什么? - MeanGreen

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接