2012-01-08 140 views
41

我有一個UINavigationBar添加到我的UIViewController視圖。我想更改字體屬性。請注意,我想更改UINavigationBar而非控制器。在我使用UINavigationController的應用程序中,我使用self.navigationItem.titleView = label;來顯示自定義標籤。更改UINavigationBar字體屬性?

如何在UINavigationBar中實現自定義標題?

P.S我用它來設置我的UINavigationBar的標題文本self.navBar.topItem.title = @"Information";

回答

86

從iOS 5開始,我們必須使用titleTextAttribute Dictionary(UInavigation控制器類參考中的預定義字典)來設置導航欄的標題文本顏色和字體。

[[UINavigationBar appearance] setTitleTextAttributes: 
    [NSDictionary dictionaryWithObjectsAndKeys: 
     [UIColor blackColor], NSForegroundColorAttributeName, 
      [UIFont fontWithName:@"ArialMT" size:16.0], NSFontAttributeName,nil]]; 

以下教程是定製UIElements(如UInavigation欄,UIsegmented控件,UITabBar)的最佳教程。這可能對你有幫助

http://www.raywenderlich.com/4344/user-interface-customization-in-ios-5

+2

@rolandjitsu你只需使用NSFontAttributeName而不是UITextAttributeFont。 – CedricSoubrie 2013-12-19 23:57:55

+1

@CedricSoubrie〜是的,這是真的,只是想告訴作者有關這個,所以我們可以有一個更新的答案:) – Roland 2013-12-20 19:08:10

+3

和UITextAttributeTextColor應該用NSForegroundColorAttributeName – bobmoff 2014-03-04 08:25:07

21

(這是不可能使用新的iOS 5.0外觀API)。

編輯:

的iOS> = 5.0:

設置標題文本屬性的導航欄:

// Customize the title text for *all* UINavigationBars 
NSDictionary *settings = @{ 
    UITextAttributeFont     : [UIFont fontWithName:@"YOURFONTNAME" size:20.0], 
    UITextAttributeTextColor   : [UIColor whiteColor], 
    UITextAttributeTextShadowColor  : [UIColor clearColor], 
    UITextAttributeTextShadowOffset  : [NSValue valueWithUIOffset:UIOffsetZero]}; 

[[UINavigationBar appearance] setTitleTextAttributes:settings]; 

的iOS 5.0 <

UINavigationItem沒有一個名爲標籤或某物的屬性,只有一個titleView。你只能通過設置自定義標籤,因爲這標題視圖 你可以使用下面的代碼來設置字體:(所建議的here

UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 44)]; 
label.font = [UIFont fontWithName:@"YOURFONTNAME" size:20.0]; 
label.shadowColor = [UIColor clearColor]; 
label.textColor =[UIColor whiteColor]; 
label.text = self.title; 
self.navigationItem.titleView = label;  
[label release]; 
+0

的確,感謝您的評論。我當時找不到它。 – Tieme 2012-12-28 14:48:33

+0

非常感謝:) – Abo3atef 2014-05-12 14:45:39

7

只是把這個在您的應用程序委託的

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 

雷Wenderlich:

http://www.raywenderlich.com/4344/user-interface-customization-in-ios-5

[[UINavigationBar appearance] setTitleTextAttributes: 
[NSDictionary dictionaryWithObjectsAndKeys: 
[UIColor colorWithRed:255.0/255.0 green:255.0/255.0 blue:255.0/255.0 alpha:1.0], 
UITextAttributeTextColor, 
[UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.8], 
UITextAttributeTextShadowColor, 
[NSValue valueWithUIOffset:UIOffsetMake(0, -1)], 
UITextAttributeTextShadowOffset, 
[UIFont fontWithName:@"STHeitiSC-Light" size:0.0], 
UITextAttributeFont, nil]]; 
+0

不要將大小0設置爲您的字體,當您推送到視圖控制器時會導致視覺錯誤。首先,當你推動字體的大小爲0時,它會適當調整自己的大小,但它仍然是可見的 - 並且令人討厭。將它設置爲18.0 – 2012-08-09 20:45:46

0

不要忘記將字體添加到您的目標。

我做了關於字體的所有事情,而且我無法加載它,實際上字體不是我的目標成員。

因此,請檢查目標成員以及添加的自定義字體。

1

下面的鏈接將幫助你。 這取決於在那裏你想目前的viewController的導航欄上的所有應用程序的任何地方設置字體屬性

你可以找到很好的教程這裏% http://www.appcoda.com/customize-navigation-status-bar-ios-7/?utm_campaign=iOS_Dev_Weekly_Issue_118&utm_medium=email&utm_source=iOS%2BDev%2BWeekly

基本的想法是創建的NSDictionary實例,並填充你想要的字體和其他屬性。 我完成了這個解決方案:

在-viewDidLoad控制器的方法[超級viewDidLoad中]把這個線後:

UIColor *color = [UIColor redColor]; 
NSShadow *shadow = [NSShadow new]; 
UIFont *font = [UIFont fontWithName:@"EnterYourFontName" size:20.0] 
shadow.shadowColor = [UIColor greenColor]; 
shadow.shadowBlurRadius = 2; 
shadow.shadowOffset = CGSizeMake(1.0f, 1.0f); 

//fill this dictionary with text attributes 
NSMutableDictionary *topBarTextAttributes = [NSMutableDictionary new]; 
    //ios 7 
topBarTextAttributes[NSForegroundColorAttributeName] = color; 
    //ios 6 
topBarTextAttributes[UITextAttributeTextColor] = color; 
    //ios 6 
topBarTextAttributes[UITextAttributeTextShadowOffset] = [NSValue valueWithCGSize:shadow.shadowOffset]; 
topBarTextAttributes[UITextAttributeTextShadowColor] = shadow.shadowColor; 
    //ios 7 
topBarTextAttributes[NSShadowAttributeName] = shadow; 
    //ios 6 
topBarTextAttributes[UITextAttributeFont] = font; 
    //ios 7 
topBarTextAttributes[NSFontAttributeName] = font; 

//for all the controllers uncomment this line 
// [[UINavigationBar appearance]setTitleTextAttributes:topBarTextAttributes]; 

//for current controller uncoment this line 
// self.navigationController.navigationBar.titleTextAttributes = topBarTextAttributes; 
4

在iOS8上的快捷,使用下面的代碼設置字體:

var navigationBarAppearance = UINavigationBar.appearance() 
let font = UIFont(name: "Open Sans", size: 17) 
if let font = font { 
    navigationBarAppearance.titleTextAttributes = [NSFontAttributeName: font, NSForegroundColorAttributeName: UIColor.whiteColor()] 
}