我正在开发一个基于tabbarController的应用。我有3个tabbar项目。
我的问题是:如何为选项卡项目上的标题更改字体样式?
我正在开发一个基于tabbarController的应用。我有3个tabbar项目。
我的问题是:如何为选项卡项目上的标题更改字体样式?
[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]];
setTitleTextAttributes:
后添加forState:
参数。然后,它将可以编译而且按预期工作。 - element119这将在整个应用程序中一次性更改您的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)
抱歉,我认为没有办法做到这一点。如果你非常需要,你需要编写自己的选项卡栏。
很遗憾,目前在iOS上无法实现这一点,除非您构建自己的自定义选项卡栏,在iOS5上使用故事板并不是很困难。
无法实现,需要通过继承UITabbar创建自定义选项卡栏
'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];
试试这个。
[[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];