2011-04-29 52 views
64

這個問題很簡單,很簡單,答案不幸。更改導航欄的字體

如何更改UINavigationBar中文字的字體?

+2

只是因爲這是我得到的第一個結果,當我去谷歌搜索主題,並有一個更新的答案:http://stackoverflow.com/a/10440362/700471 – 2012-10-12 17:59:41

+0

我相信有一個區別兩個問題。在我要求更改單個導航欄的字體的地方,另一個問題是關於更改「全部」導航欄的字體。儘管如此,這是一個有用的鏈接,可以提及其他人可能感興趣的內容! – Joetjah 2012-10-22 12:07:48

+7

對於單個導航欄,請使用:[self.navigationController。navigationBar setTitleTextAttributes:@ {UITextAttributeFont:[UIFont fontWithName:<#(NSString *)#> size:<#(CGFloat)#>]}];' – Philip007 2012-11-09 11:56:14

回答

157

距離Ios 7和更高:

NSShadow* shadow = [NSShadow new]; 
shadow.shadowOffset = CGSizeMake(0.0f, 1.0f); 
shadow.shadowColor = [UIColor redColor]; 
[[UINavigationBar appearance] setTitleTextAttributes: @{ 
    NSForegroundColorAttributeName: [UIColor greenColor], 
       NSFontAttributeName: [UIFont fontWithName:@"Helvetica" size:20.0f], 
       NSShadowAttributeName: shadow 
                 }]; 

距離Ios 5和更高:

[[UINavigationBar appearance] setTitleTextAttributes: @{ 
           UITextAttributeTextColor: [UIColor greenColor], 
          UITextAttributeTextShadowColor: [UIColor redColor], 
         UITextAttributeTextShadowOffset: [NSValue valueWithUIOffset:UIOffsetMake(0.0f, 1.0f)], 
            UITextAttributeFont: [UIFont fontWithName:@"Helvetica" size:20.0f] 
    }]; 

比iOS 5的早些時候:

UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 400, 44)]; 
label.backgroundColor = [UIColor clearColor]; 
label.font = [UIFont boldSystemFontOfSize:20.0]; 
label.shadowColor = [UIColor colorWithWhite:0.0 alpha:0.5]; 
label.textAlignment = UITextAlignmentCenter; 
label.textColor =[UIColor whiteColor]; 
label.text=self.title; 
self.navigationItem.titleView = label;  
[label release]; 
+0

當您向下鑽取並返回時,消失... – ozba 2012-11-05 11:13:28

+1

請提醒這個答案已經過時。有很多簡單的方法可以在iOS 5和6中自定義UI元素的外觀。只需使用「外觀」代理即可。 – Philip007 2012-11-09 11:59:19

+0

如何讓此代碼使用SystemFont而不是硬編碼到Helvetica? – Brabbeldas 2015-11-01 16:54:20

5

以上答案有效。我會在最後一行之前添加以下行。如果我不這樣做,看起來標籤如果在左側有後退按鈕但沒有右側按鈕,則中心對齊方式不正確。

... 
[self.navigationItem.titleView sizeToFit]; 
[label release]; // not needed if you are using ARC 
5

更新了iOS的7:

[[UINavigationBar appearance] setTitleTextAttributes: [NSDictionary dictionaryWithObjectsAndKeys: 
                 [UIColor colorWithRed:245.0/255.0 green:245.0/255.0 blue:245.0/255.0 alpha:1.0], NSForegroundColorAttributeName, 
                 shadow, NSShadowAttributeName, 
                 [UIFont fontWithName:@"HelveticaNeue-CondensedBlack" size:21.0], NSFontAttributeName, nil]]; 

禮節:

http://www.appcoda.com/customize-navigation-status-bar-ios-7/

1
 NSShadow *shadow = [NSShadow new]; 
[shadow setShadowColor: [UIColor clearColor]]; 
[shadow setShadowOffset: CGSizeMake(0.0f, 1.0f)]; 

[self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys: 
             [UIFont fontWithName:@"TimeBurner" size:27.0f], NSFontAttributeName, 
             [UIColor whiteColor], NSForegroundColorAttributeName, 
             shadow, NSShadowAttributeName,nil]]; 
+0

我已經下載了名爲timeburner_regular.ttf的字體,所以我寫了上面的代碼。它爲我工作。 – Vin 2014-03-06 14:06:33

15

如果你想改變Interface Builder本身的字體(沒有任何代碼)這裏是做在Xcode6方式:

1)查找下導航控制器場景 enter image description here

2.導航欄視圖)更改標題字體,顏色和陰影的屬性屬性督察。 enter image description here

4

不知道爲什麼所有的答案都包含了陰影。添加操縱陰影的線不會改變文字字體。這些2行的代碼會爲的iOS 8.4工作和夫特

let attributesDictionary = [NSFontAttributeName: UIFont(name: "Helvetica Neue", size: 14)!] 
navigationController!.navigationBar.titleTextAttributes = attributesDictionary 

titleTextAttributes存儲的字典,將決定的字體,顏色,大小,和導航欄的標題的其它屬性。

+0

而文本的默認顏色將是'UINavigationBar'的'tintColor'。如果您想要不同的東西,請設置色調顏色,或者將「NSForegroundColorAttributeName」屬性和值添加到該屬性字典中。 – NRitH 2015-08-24 00:44:40

+0

如何讓這個使用SystemFont? – Brabbeldas 2015-11-01 16:53:47