2014-02-19 52 views
14

我使用的字體真棒資源爲我的iPhone應用程序的UI: FontAwesome如何使用自定義字體在iOS的標籤欄

我有我的應用程序界面中使用它像如下:

Phone.font = [UIFont fontWithName:kFontAwesomeFamilyName size:40]; 
Phone.text = [NSString fontAwesomeIconStringForIconIdentifier:@"fa-phone"]; 

但現在我想在我的標籤欄控制器的標籤欄項目中使用它,即我想設置標籤欄的圖標爲字體真棒元素。如何才能做到這一點?

+0

是這樣解決了嗎? – headkit

回答

2

試試這個代碼:

[[UITabBarItem appearance] setTitleTextAttributes: @{NSForegroundColorAttributeName: TAB_BAR_TEXT_NORMAL_COLOR, NSFontAttributeName: [UIFont fontWithName:CUSTOM_FONT_NAME size:TAB_BAR_FONT_SIZE]} forState:UIControlStateNormal]; 
[[UITabBarItem appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor whiteColor], NSFontAttributeName: [UIFont fontWithName:CUSTOM_FONT_NAME size:TAB_BAR_FONT_SIZE]} forState:UIControlStateSelected]; 
2

由於iOS7 UITextAttributeFont已過時,使用NSFontAttributeName

[[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIFont fontWithName:@"your_font_name" size:20.0f], NSFontAttributeName, nil] forState:UIControlStateNormal]; 
9

斯威夫特版本:

UITabBarItem.appearance().setTitleTextAttributes(
      [NSFontAttributeName: UIFont(name:"Ubuntu", size:11)!, 
       NSForegroundColorAttributeName: UIColor(rgb: 0x929292)], 
      forState: .Normal) 
+0

從6.1開始,你必須打開字體。放下!在你的字體初始化後 – hash3r

+0

謝謝,是的,它是從'Swift 1.2'開始的。編輯。 –

1

使用這種它完美...

UIView *iv = [[UIView alloc] initWithFrame:CGRectMake(0,0,230.0,80.0)]; 
UILabel *lbl = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 230, 80)]; 
lbl.textColor = [UIColor whiteColor]; 
lbl.font = [UIFont fontWithName:@"Ubuntu" size:18.0f]; 
lbl.textAlignment = NSTextAlignmentCenter; 
lbl.text = [NSString stringWithFormat:@"%lu Store Found",(unsigned long)arrOfferList.count]; 
[iv addSubview:lbl]; 
self.tabBarController.navigationItem.titleView = iv; 
2

我無法使用接受的答案更改字體。這是我在

didFinishLaunchingWithOptions
方法的AppDelegate以後使用:

 UITabBarController *tabBarController = (UITabBarController *)self.window.rootViewController; 
    UITabBar *tabBar = tabBarController.tabBar; 
    UITabBarItem *tabBarItem1 = [tabBar.items objectAtIndex:0]; 
    UITabBarItem *tabBarItem2 = [tabBar.items objectAtIndex:1]; 

    [tabBarItem1 setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:17 weight:UIFontWeightBold], NSFontAttributeName, nil] forState:UIControlStateSelected]; 

    [tabBarItem2 setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:17 weight:UIFontWeightBold], NSFontAttributeName, nil] forState:UIControlStateSelected]; 

    [tabBarItem1 setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:17 weight:UIFontWeightBold], NSFontAttributeName, nil] forState:UIControlStateNormal]; 

    [tabBarItem2 setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:17 weight:UIFontWeightBold], NSFontAttributeName, nil] forState:UIControlStateNormal];

點擊此處瞭解詳情:http://www.appcoda.com/ios-programming-how-to-customize-tab-bar-background-appearance/

5

斯威夫特3

UITabBarItem.appearance().setTitleTextAttributes([NSFontAttributeName: UIFont(name:"Latio-Regular", size:14)!, NSForegroundColorAttributeName: UIColor.white], for: .normal) 
相關問題