web-dev-qa-db-fra.com

Changer la police de la barre de navigation

La question est simple et simple, la réponse n'est malheureusement pas.

Comment pouvez-vous changer la police du texte dans le UINavigationBar?

64
Joetjah

Depuis iOS 7 et versions ultérieures:

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
                                                      }];

Depuis iOS 5 et versions ultérieures:

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

Avant 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];
158
Aravindhan

Si vous vouliez changer la police dans l'Interface Builder lui-même (sans aucun code) voici la façon de le faire dans Xcode6:

1.) Trouvez la vue Barre de navigation sous la scène du contrôleur de navigation enter image description here

2.) Modifiez les attributs Police du titre, Couleur et Ombre dans l'inspecteur des attributs. enter image description here

15
inder

Mis à jour pour 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]];

courtoisie de:

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

5
AbuZubair

La réponse ci-dessus fonctionne. J'ajouterais la ligne suivante avant la dernière ligne. Si je ne le fais pas, il semble que l'étiquette soit mal alignée au centre s'il y a un bouton de retour sur le côté gauche mais pas de bouton droit.

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

Je ne sais pas pourquoi toutes les réponses incluaient l'ombre. L'ajout des lignes qui manipulent l'ombre ne change rien à la modification de la police du texte. Ces 2 lignes de code fonctionneront pour iOS 8.4 et Swift

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

titleTextAttributes stocke un dictionnaire qui dictera la police, la couleur, la taille et d'autres attributs du titre de la barre de navigation.

4
Kelvin Lau

Depuis iOS 5, vous pouvez utiliser le proxy d'apparence.

La réponse est en double de cette question: https://stackoverflow.com/a/12364740/88341

1
Ric Santos
     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]];
1
Vin