是否可以更改制表符的字體大小?更改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