是否可以更改制表符的字体大小?更改tabbaritem的字体大小
回答
其实有一种方法。
NSMutableArray *tabBarItems = [[[[[self.view subviews] objectAtIndex:1] subviews] mutableCopy] autorelease];
for (int item = 0; item < [tabBarItems count]; item++) {
for (int subview = 0; subview < [[[tabBarItems objectAtIndex:item] subviews] count]; subview++) {
for (int item = 0; item < [tabBarItems count]; item++) {
for (int subview = 0; subview < [[[tabBarItems objectAtIndex:item] subviews] count]; subview++) {
if ([[[[tabBarItems objectAtIndex:item] subviews] objectAtIndex:subview] isKindOfClass:NSClassFromString(@"UITabBarButtonLabel")])
[[[[tabBarItems objectAtIndex:item] subviews] objectAtIndex:subview] setFont:[UIFont systemFontOfSize:6.0f]];
}
}
}
}
TabBarIncreaseFonts(self.tabBarController);
void TabBarIncreaseFonts(UITabBarController* customTabBarController)
{
for(UIView* controlLevelFirst in [customTabBarController.tabBar subviews])
{
if(![controlLevelFirst isKindOfClass:NSClassFromString(@"UITabBarButton")])
continue;
for(id controlLevelSecond in [controlLevelFirst subviews])
{
[controlLevelSecond setBounds: CGRectMake(0, 0, 100, 48)];
if(![controlLevelSecond isKindOfClass:NSClassFromString(@"UITabBarButtonLabel")])
continue;
[controlLevelSecond setFont: [UIFont boldSystemFontOfSize:20]];
[controlLevelSecond setFrame: CGRectMake(0, 0, 96, 48)];
[controlLevelSecond setTextAlignment:UITextAlignmentCenter];
}
}
}
我推荐一个更好的办法:
[yourTabBarItem setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
[UIColor whiteColor], UITextAttributeTextColor,
[NSValue valueWithUIOffset:UIOffsetMake(0,0)], UITextAttributeTextShadowOffset,
[UIFont fontWithName:@"Helvetica" size:18.0], UITextAttributeFont, nil]
forState:UIControlStateNormal];
for(UIViewController *tab in self.tabBarController.viewControllers)
{
[tab.tabBarItem setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
[UIFont fontWithName:@"Helvetica" size:20.0], UITextAttributeFont, nil]
forState:UIControlStateNormal];
}
更新:请参阅voghDev对iOS 7.0+版本的回答,它避免了弃用的UITextAttributeFont。 – ToolmakerSteve 2017-02-16 19:07:29
[离开这个在这里为自己的参考,只是一个即兴过其他工作的答案。对于MEM它适用于iOS 7的修复,这超出的提问有点...]
@implementation UITabBarController (Util)
- (void) fixForIos7 {
if (!IS_IOS7)
return;
UIFont *tabBarFont = [UIFont systemFontOfSize:12];
NSDictionary *titleTextAttributes = [NSDictionary dictionaryWithObjectsAndKeys:
tabBarFont, UITextAttributeFont, nil];
for(UIViewController *tab in self.viewControllers) {
[tab.tabBarItem setTitleTextAttributes:titleTextAttributes forState:UIControlStateNormal];
}
}
@end
缺少方法是
#define IS_IOS7 (UIDevice.currentDevice.systemVersion.floatValue > 6.9)
简单的iOS中5.0或更高版本:
[[UITabBarItem appearance] setTitleTextAttributes:@{UITextAttributeFont:[UIFont boldSystemFontOfSize:15]} forState:UIControlStateNormal];
好的解决方案,但使用'NSFontAttributeName'而不是'UITextAttributeFont' – Laszlo 2016-08-30 09:43:05
[更新] iOS 7.0以上版本的版本@ cancer86的很好的回答:
[[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
[UIColor whiteColor], NSForegroundColorAttributeName,
[UIFont fontWithName:@"Helvetica" size:tabFontSize],
NSFontAttributeName,
nil] forState:UIControlStateNormal];
[[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
[UIColor redColor], NSForegroundColorAttributeName,
[UIFont fontWithName:@"Helvetica" size:tabFontSize], NSFontAttributeName,
nil] forState:UIControlStateSelected];
主要的变化是,UITextAttributeTextColor和UITextAttributeFont都不赞成
为了将其应用到所有标签(感谢@ToolmakerSteve您指出)
for(UIViewController *tab in self.tabBarController.viewControllers)
{
[tab.tabBarItem setTitleTextAttributes: ...];
}
..应用于所有标签,从spatil的答案'for(UIViewController * tab in self.tabBarController.viewControllers)[tab.tabBarItem setTitleTextAttributes:...' – ToolmakerSteve 2016-12-21 21:26:44
好的反应更新 – voghDev 2016-12-22 08:45:25
我们可以有快速的3版本 – 2017-02-03 12:04:42
斯威夫特2.0
override func viewDidLoad() {
super.viewDidLoad()
let appearance = UITabBarItem.appearance()
let attributes: [String: AnyObject] = [NSFontAttributeName:UIFont(name: "American Typewriter", size: 12)!, NSForegroundColorAttributeName: UIColor.orangeColor()]
appearance.setTitleTextAttributes(attributes, forState: .Normal)
}
In Swift 3.0
override func viewDidLoad() {
super.viewDidLoad()
let appearance = UITabBarItem.appearance()
let attributes: [String: AnyObject] = [NSFontAttributeName:UIFont(name: "American Typewriter", size: 12)!, NSForegroundColorAttributeName: UIColor.orange]
appearance.setTitleTextAttributes(attributes, for: .normal)
}
- 1. 更改Highcharts字体大小
- 2. getOrgChart字体大小更改
- 3. 更改字体大小
- 4. rtf更改字体大小
- 5. 字体大小更改
- 6. NSAttributedString更改字体大小
- 7. 更改字体大小macvim?
- 8. CFDocumentItem - 更改字体大小
- 9. 字体和字体大小更改
- 10. 更改Div字体大小和字体
- 11. 更改字体大小而不更改框的大小
- 12. 自动更改主体字体大小
- 13. 更改字体大小缩小级别
- 14. 更改GridView的ColumnHeader的字体大小
- 15. 更改字体大小调整和更改文件的高度时,字体大小的更改
- 16. 如何更改GridviewColumnHeader的字体大小?
- 17. 更改按钮上的字体大小
- 18. 更改axisLabel的字体大小
- 19. 更改C中的ContextMenu字体大小#
- 20. UISlider更改UITextView的字体大小
- 21. 更改JavaFX TableView的字体大小
- 22. 更改字体大小为jfreechart的
- 23. 更改字体大小上的DataGridView C#
- 24. 更改Emacs中的字体大小cli
- 25. 更改JTextArea字体大小的问题
- 26. 更改PanoramaItem的字体大小(windows phone)
- 27. 更改屏幕中的字体大小
- 28. 更改特定字体的大小
- 29. 更改字体大小jQuery的Hightcharts
- 30. 无法更改@media的字体大小
它只能在iOS5.0(或更高版本).. – Kjuly 2011-12-28 05:37:37
不错,它的作品精美绝伦,我..谢谢 – Gaurav 2012-07-22 14:22:47
哪个地方你需要写上面这段代码?我尝试在[self.tabBarController setViewControllers:aControllerList animated:YES]之后使用它;但这并没有帮助。 – Abhinav 2012-09-17 21:46:16