2014-11-24 54 views
0

我想讓我的標籤欄項目的字體大小更大。我曾嘗試在AppDelegate中這樣做:何處/何時/如何設置tabBarItem字體屬性屬性?

MenuTableViewController *mvc = [[MenuTableViewController alloc] init]; 
mvc.delegate = self; 
mvc.restorationIdentifier = NSStringFromClass([mvc class]); 
mvc.restorationClass = [self class]; 
UINavigationController *nav1 = [[UINavigationController alloc] initWithRootViewController:mvc]; 
SuperTabBarController *nav = [[SuperTabBarController alloc] init]; 
JudgeViewController *jvc = [[JudgeViewController alloc] init]; 
BuzzViewController *bvc = [[BuzzViewController alloc] init]; 
nav.viewControllers = @[jvc, bvc, nav1]; 

UIFont *tabBarFont = [UIFont systemFontOfSize:40]; 
NSDictionary *titleTextAttributes = [NSDictionary dictionaryWithObjectsAndKeys: 
            tabBarFont, NSFontAttributeName, nil]; 
for(UIViewController *tab in nav.viewControllers) { 
    [tab.tabBarItem setTitleTextAttributes:titleTextAttributes forState:UIControlStateNormal]; 
    NSLog(@"yo normal"); 
} 

for(UIViewController *tab in nav.viewControllers) { 
    [tab.tabBarItem setTitleTextAttributes:titleTextAttributes forState:UIControlStateSelected]; 
    NSLog(@"yo");  
} 


[UIView transitionWithView:self.window duration:0.5 options:UIViewAnimationOptionTransitionFlipFromLeft animations:^{self.window.rootViewController = nav;} completion:nil]; 

我也試圖定義字典,像這樣:

NSDictionary *titleTextAttributes2 = @{NSFontAttributeName: 
               [UIFont fontWithName:@"Helvetica" size:20]}; 

這是在通過委託協議,一旦用戶登錄時調用自定義函數我NSLogs打印出來,但我也得到這個控制檯:

button text attributes only respected for UIControlStateNormal, UIControlStateSelected and UIControlStateDisabled. state = 1 is interpreted as UIControlStateSelected. 

程序根據需要比標題字體不都用這個命令,儘管日誌outpu改變其他運行TS。

於是我把我的自定義視圖控制器這裏面viewDidLoad中:

UIFont *tabBarFont = [UIFont systemFontOfSize:40]; 
NSDictionary *titleTextAttributes = [NSDictionary dictionaryWithObjectsAndKeys: 
            tabBarFont, NSFontAttributeName, nil]; 

    [self.tabBarItem setTitleTextAttributes:titleTextAttributes forState:UIControlStateNormal]; 
    [self.tabBarItem setTitleTextAttributes:titleTextAttributes forState:UIControlStateSelected]; 

但這也絲毫不影響標籤欄標題的字體大小。

我應該在哪裏執行這些命令?

謝謝。


編 被標記的答案做什麼,我需要,但我注意到,該方法僅工作,如果我使用類範圍的制定者,而不是一個實例的制定者。因此,要全局更改標籤欄項目的標題文字,這就像一個魅力:

NSDictionary *attribute =  @{NSFontAttributeName: 
            [UIFont fontWithName:@"Helvetica" size:40]}; 
[[UITabBarItem appearance] setTitleTextAttributes:attribute forState:UIControlStateNormal]; 

努力仍然設置一個類實例的屬性不工作(這些被稱爲都在同一個功能,但只有一個生效)。 NSDictionary * attribute2 = @ {NSFontAttributeName: [UIFont fontWithName:@「Helvetica」size:10]}; [nav1.tabBarItem setTitleTextAttributes:attribute2 forState:UIControlStateNormal];

任何人都知道這是爲什麼?我多次瀏覽了Apple Dev文檔,卻沒有看到爲什麼類實例調用沒有做任何事情。

回答

0

我在didFinishLaunchingWithOptions中使用過這段代碼,它對我很有用。

NSDictionary *attribute = @{NSForegroundColorAttributeName:[UIColor whiteColor]}; 
[[UITabBarItem appearance] setTitleTextAttributes:attribute forState:UIControlStateNormal]; 
+0

謝謝!非常感激。 – 2014-11-24 01:29:19