![](/img/trans.png)
[英]How to change default the gray color of uitabbaritem in uitabbarcontroller?
[英]How to change the Color of text in UITabBarItem in iOS 5
在iOS 5中具有更多外觀控制,我們如何更改UITabBarItem文本顏色? 從默認白色到其他顏色?
編輯:工作解決方案
[[UITabBarItem appearance] setTitleTextAttributes:
[NSDictionary dictionaryWithObjectsAndKeys:
[UIColor blackColor], UITextAttributeTextColor,
[UIColor whiteColor], UITextAttributeTextShadowColor,
[NSValue valueWithUIOffset:UIOffsetMake(0, 1)], UITextAttributeTextShadowOffset,
[UIFont fontWithName:@"Rok" size:0.0], UITextAttributeFont,
nil]
forState:UIControlStateNormal];
你是說這個嗎? 請記住,這僅適用於iOS5.0或更高版本。
if ([self.tabBarItem respondsToSelector:@selector(setTitleTextAttributes:)]) {
NSLog(@"*** Support method(iOS 5): setTitleTextAttributes:");
[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]];
}
Apple關於自定義外觀的文檔:
在iOS v5.0及更高版本中,您可以通過使用UIBarItem聲明的外觀選擇器設置項目標簽文本屬性來自定義選項卡欄的外觀。 您還可以使用“自定義外觀”中列出的方法。您可以使用外觀代理(例如,[UITabBarItem外觀])或單個選項卡欄自定義所有分段控件的外觀。 您還可以使用“管理完成的所選圖像”中列出的方法提供已完成的選定圖像和未選擇圖像; 但是,這些方法不參與UIAppearance代理API(參見UIAppearance)。 UIKit現在為成品圖像提供任何自動處理。 為了獲得良好的結果,您必須使用setFinishedSelectedImage:withFinishedUnselectedImage:在匹配對中提供已完成的已選擇和未選擇的圖像。
編輯:這是使用UIAppearance系統和NSDictionary文字語法的另一個例子:
[[UITabBarItem appearance] setTitleTextAttributes:@{
UITextAttributeFont : [UIFont fontWithName:@"AmericanTypewriter" size:20.0f],
UITextAttributeTextColor : [UIColor blackColor],
UITextAttributeTextShadowColor : [UIColor grayColor],
UITextAttributeTextShadowOffset : [NSValue valueWithUIOffset:UIOffsetMake(0.0f, 1.0f)]}];
編輯 (由@JeremyWiebe撰寫):從iOS 6開始,字典鍵已更改為與OS X使用的相同:
NSShadow *shadow = [[NSShadow alloc] init];
shadow.shadowColor = [UIColor grayColor];
shadow.shadowOffset = CGSizeMake(0, 1.0);
[[UITabBarItem appearance] setTitleTextAttributes:@{
NSFontAttributeName : [UIFont fontWithName:@"AmericanTypewriter" size:20.0f],
NSForegroundColorAttributeName : [UIColor blackColor],
NSShadowAttributeName : shadow }];
[[UITabBarItem appearance] setTitleTextAttributes:@{
UITextAttributeFont : [UIFont fontWithName:@"HelveticaNeue-Bold" size:10.0f],
UITextAttributeTextColor : [UIColor colorWithRed:0/255.0 green:48/255.0 blue:92/255.0 alpha:1.0],}
forState:UIControlStateNormal];
[[UITabBarItem appearance] setTitleTextAttributes:@{
UITextAttributeFont : [UIFont fontWithName:@"HelveticaNeue-Bold" size:10.0f],
UITextAttributeTextColor : [UIColor colorWithRed:0/255.0 green:138/255.0 blue:196/255.0 alpha:1.0],}
forState:UIControlStateSelected];
UITextAttributeFont,UITextAttributeTextColor等在iOS 7.0中已棄用。
你必須使用:
NSFontAttributeName, NSParagraphStyleAttributeName, NSForegroundColorAttributeName, NSBackgroundColorAttributeName, NSLigatureAttributeName, NSKernAttributeName, NSStrikethroughStyleAttributeName, NSUnderlineStyleAttributeName, NSStrokeColorAttributeName, NSStrokeWidthAttributeName, NSShadowAttributeName and NSVerticalGlyphFormAttributeName
特別是對於iOS 7,嘗試使用NSForegroundColorAttributeName而不是UITextAttributeTextColor
適用於iOS 7.0+的工作解決方案:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
[UIColor redColor], NSForegroundColorAttributeName,
nil] forState:UIControlStateNormal];
[[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
[UIColor whiteColor], NSForegroundColorAttributeName,
nil] forState:UIControlStateSelected];
}
我沒有足夠的聲譽點來添加評論,所以我將在這里添加另一個答案。
我遇到了同樣的問題並搜索過去一小時,最后意識到我的問題是因為我沒有將代碼放入方法viewWillAppear中 。 不確定這是否是常識,因為我剛開始使用objective-c但是認為這應該是答案的另一條重要信息,因為相同的代碼在viewDidLoad中不起作用。
根據這篇文章 ,這個代碼只有在方法viewWillAppear中才有效。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.