2012-01-08 13 views
41

Ho aggiunto una UINavigationBar alla vista di UIViewController. Voglio cambiare le proprietà dei caratteri. Si noti che voglio cambiare un UINavigationBar non controller. Nella mia app in cui utilizzo UINavigationController, utilizzo self.navigationItem.titleView = label; per visualizzare l'etichetta personalizzata.Modificare le proprietà dei font UINavigationBar?

Come posso ottenere un titolo personalizzato nella mia UINavigationBar?

P.S Io lo uso per impostare il testo del titolo self.navBar.topItem.title = @"Information"; della mia UINavigationBar.

risposta

86

Da iOS 5 in poi dobbiamo impostare il colore del testo del titolo e il carattere della barra di navigazione usando titleTextAttribute Dictionary (dizionario predefinito in riferimento alla classe controller di UInavigation).

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

Il tutorial seguito è la migliore tutorial per la personalizzazione di UIElements come bar UInavigation, UIsegmented controllo, UITabBar. Questo può essere utile a voi

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

+2

@rolandjitsu devi solo usare NSFontAttributeName invece di UITextAttributeFont. – CedricSoubrie

+1

@CedricSoubrie ~ sì, è vero, volevo solo informare l'autore di ciò in modo che possiamo avere una risposta aggiornata :) – Roland

+3

E UITextAttributeTextColor deve essere sostituito con NSForegroundColorAttributeName – bobmoff

21

(Questo non è possibile utilizzando la nuova API di Appearance iOS 5.0).

Edit:

iOS> = 5,0:

Impostare il titolo attributi di testo per il Navigationbar:

// 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 non hai una proprietà chiamata etichetta o qualcosa, solo una titleView. Tu sei solo in grado di impostare il tipo di carattere impostando un'etichetta personalizzata da questo punto di vista titolo è possibile utilizzare il seguente codice: (come suggerito 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

Infatti, grazie per il commento. Non riuscivo a trovarlo in quel momento. – Tieme

+0

GRAZIE PER FAVORE :) – Abo3atef

7

Basta mettere questo nella vostra applicazione del delegato

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

Da Ray 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

Non impostare la dimensione 0 sul carattere, che causerà errori visivi quando si preme su un controller di visualizzazione. Innanzitutto quando si preme il font avrà la dimensione 0 e quindi si ridimensionerà in modo appropriato ma è ancora visibile e fastidioso. Impostalo su 18.0 –

0

Non dimenticate di aggiungere il font al vostro obiettivo.

Ho fatto tutto ciò che si può fare su un font e non ho potuto caricarlo, quando, infatti, il font non era un membro del mio target.

Pertanto, verificare l'appartenenza di destinazione e il carattere personalizzato aggiunto.

1

collegamento qui sotto ti aiuterà. Dipende da dove si desidera impostare le proprietà dei font sulla barra di navigazione in corso di viewController tutto in qualsiasi applicazione

potete trovare bel tutorial qui% 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

L'idea di base è quella di creare un'istanza NSDictionary e riempirlo con il carattere desiderato e altre proprietà. ho finito con questa soluzione:

nel metodo di controllo -viewDidLoad dopo [super viewDidLoad] mettere queste righe:

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

In iOS 8 rapida, utilizzare il seguente codice per impostare font:

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