2012-05-03 12 views
89

Ho un multiple View Controller e voglio impostare il colore del carattere di tutti su rosso.Come impostare il carattere e il colore del titolo in UINavigationBar usando l'API di aspetto iOS5?

[[UINavigationBar appearance] setFont:[UIFont boldSystemFontOfSize:12.0]]; 

lancia un errore di selezione non riconosciuto.

Si prega di fornire un codice, per aiutare a risolvere il seguente.

+0

allora si dovrebbe impostare l'etichetta e la vista sulla barra di navigazione ans impostare il colore del carattere – vishiphone

risposta

207

Da Ray Wenderlich:

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

// Customize the title text for *all* UINavigationBars 
[[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:@"Arial-Bold" size:0.0], 
     UITextAttributeFont, 
     nil]]; 

O se preferite con l'oggetto stile letterale:

[[UINavigationBar appearance] setTitleTextAttributes:@{ 
    UITextAttributeTextColor: [UIColor colorWithRed:255.0/255.0 green:255.0/255.0 blue:255.0/255.0 alpha:1.0], 
    UITextAttributeTextShadowColor: [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.8], 
    UITextAttributeTextShadowOffset: [NSValue valueWithUIOffset:UIOffsetMake(0, -1)], 
    UITextAttributeFont: [UIFont fontWithName:@"Arial-Bold" size:0.0], 
}]; 

Modifica per iOS 7 e seguenti

UITextAttributes un rosso eprecate come iOS 7 è possibile utilizzare il seguente:

NSShadow *shadow = [[NSShadow alloc] init]; 
shadow.shadowColor = [UIColor colorWithWhite:.0f alpha:1.f]; 
shadow.shadowOffset = CGSizeMake(0, -1); 

[[UINavigationBar appearance] setTitleTextAttributes:@{ 
    NSForegroundColorAttributeName: [UIColor whiteColor], 
    NSShadowAttributeName: shadow, 
    NSFontAttributeName: [UIFont fontWithName:@"Arial-Bold" size:15.0f] 
    }]; 
+1

Per oltre 1 ora, stavo lottando con quella parte di codice dal tutorial, che porta a ritagliare il titolo del mio elemento di navigazione dopo la prima pressione ...Fai attenzione alla dimensione del carattere, che era il problema nella mia situazione ... Apparentemente la dimensione 0.0 non funzionava per me ... – Bartu

+3

il nuovo dizionario letterale lo renderà molto più carino – tybro0103

+0

Solo come nota a margine questo è solo 5.0+ , se stai supportando 4 allora vale la pena di fare 'if ([navBarInstance respondsToSelector: @selector (appearance)])' prima perché si bloccherà le versioni iOS sotto 5. –

-5

Utilizzare questa riga di codice

UILabel *badge_Label=[[UILabel alloc]initWithFrame:CGRectMake(5,3, 15, 15)]; 
badge_Label.backgroundColor=[UIColor redcolor]; 
badge_Label.font=[UIFont systemFontOfSize:12]; 
[badge_Label setText:@"20"]; 
[self.navigationController.navigationBar addSubview:badgelabel]; 

penso che questo sarà utile per voi.

+0

so che questo, stavo cercando di usare la funzione proxy dell'aspetto API – carbonr

+0

ok quindi scusate non lo so perché è in ios5 solo – vishiphone

22

Per le destinazioni di distribuzione maggiori o uguali a iOS 6, è necessario utilizzare NSShadow invece:

NSShadow * shadow = [[NSShadow alloc] init]; 
shadow.shadowColor = [UIColor lightGrayColor]; 
shadow.shadowOffset = CGSizeMake(0, -2); 

NSDictionary * navBarTitleTextAttributes = 
@{ NSForegroundColorAttributeName : [UIColor redColor], 
    NSShadowAttributeName   : shadow, 
    NSFontAttributeName   : [UIFont systemFontOfSize:14] }; 

[[UINavigationBar appearance] setTitleTextAttributes:navBarTitleTextAttributes]; 

enter image description here

5

Ho fatto questo aggiungendo solo poche righe di codice in AppDelegate.m classe didFinishLaunchingWithOptions metodo: Utilizzare questo codice:

NSDictionary *navbarTitleTextAttributes = [NSDictionary dictionaryWithObjectsAndKeys: 
              [UIColor colorWithRed:255.0f/255.0f green:0.0f/255.0f blue:0.0f/255.0f alpha:1.0],UITextAttributeTextColor, 
              [UIColor clearColor], UITextAttributeTextShadowColor, 
              [NSValue valueWithUIOffset:UIOffsetMake(-1, 0)], UITextAttributeTextShadowOffset, nil]; 

[[UINavigationBar appearance] setTitleTextAttributes:navbarTitleTextAttributes]; 

funziona per me ...

18

Fare questo su iOS 8+ e in Swift. Non c'è un setTitleTextAttributes per l'oggetto di aspetto. Invece, fare questo:

UINavigationBar.appearance().titleTextAttributes = [NSFontAttributeName : AppTheme.fontWithSize(18)] 
1

Questo potrebbe essere utilizzato per impostare una visualizzazione personalizzata per un singolo navigationBar invece di impostare un globale

- (void)updateTitleWithString:(NSString *)title 
{ 
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectZero]; 
    [headerView setAutoresizingMask:UIViewAutoresizingFlexibleWidth]; 
    [headerView setAutoresizesSubviews:YES]; 

    CGFloat headFontSize = (IS_SYSTEM_DEVICE_IPAD ? 25.0f : 19.0f); 
    UIFont *headFont = [UIFont boldSystemFontOfSize: headFontSize ]; 

    NSMutableParagraphStyle *style = [[NSParagraphStyle defaultParagraphStyle] mutableCopy]; 
    [style setLineBreakMode:NSLineBreakByTruncatingTail]; 

    CGSize size = [title boundingRectWithSize:CGSizeMake(190,headFontSize + 6) options:NSStringDrawingUsesLineFragmentOrigin 
            attributes:@{NSFontAttributeName : headFont, NSParagraphStyleAttributeName : style} context:nil].size; 

    headerView.frame = CGRectMake(0, 0,size.width,self.navigationController.navigationBar.frame.size.height); 
    float labelHeight = headFontSize + 6; 
    float labelYLoc = ( self.navigationController.navigationBar.frame.size.height - labelHeight)/2; 
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0,labelYLoc, size.width,labelHeight)]; 
    label.backgroundColor = [UIColor clearColor]; 
    label.adjustsFontSizeToFitWidth = YES; 
    label.textAlignment = NSTextAlignmentCenter; 
    label.textColor = [UIColor whiteColor]; 
    label.font = headFont; 
    label.text = title; 
    label.shadowColor = [UIColor colorWithWhite:0.0 alpha:0.4]; 
    label.lineBreakMode = NSLineBreakByTruncatingTail; 
    label.shadowOffset = CGSizeMake(0,-1); 
    label.accessibilityLabel = @"<LABEL>"; 
    [headerView addSubview:label]; 

    self.navigationItem.titleView = headerView; 
} 
5

Se avete bisogno di fare questo in Swift, è possibile creare un estensione per UINavigationBar per consentire di ottenere o impostare queste impostazioni.

extension UINavigationBar { 
var titleColor: UIColor? { 
    get { 
     if let attributes = self.titleTextAttributes { 
      return attributes[NSForegroundColorAttributeName] as? UIColor 
     } 
     return nil 
    } 
    set { 
     if let value = newValue { 
      self.titleTextAttributes = [NSForegroundColorAttributeName: value] 
     } 
    } 
} 

var titleFont: UIFont? { 
    get { 
     if let attributes = self.titleTextAttributes { 
      return attributes[NSFontAttributeName] as? UIFont 
     } 
     return nil 
    } 
    set { 
     if let value = newValue { 
      self.titleTextAttributes = [NSFontAttributeName: value] 
     } 
    } 
    } 
} 

È quindi possibile impostare il colore e il tipo di carattere in questo modo:

navigationBar.titleColor = UIColor.redColor() 
navigationBar.titleFont = UIFont.systemFontOfSize(12) 
Problemi correlati