2012-09-06 51 views
21

agrego un botón de la barra de la barra de navegación programitically de la siguiente maneraCómo cambiar el color de la fuente de color/texto de la UIBarButtonItem en la barra de navegación

UIBarButtonItem *cancel = [[UIBarButtonItem alloc] initWithTitle:@"CANCEL" style:UIBarButtonItemStyleBordered target:self action:@selector(goToPreviousView)]; 
    self.navigationItem.leftBarButtonItem = cancel; 

Ahora quiero mostrar texto "Cancelar" en color rojo.

Quiero decir que necesito cambiar el texto en los elementos del botón de barra, pero no el color del tinte del botón.

¿Cómo hacer eso?

+0

visita estos links http://stackoverflow.com/questions/3314035/how-can-i-change -the-font-color-of-a-uibarbutton-item y http://stackoverflow.com/questions/664930/uibarbuttonitem-with-color – IronManGill

+0

Para iOS 5+, vea esta respuesta: http: // stackoverflow.com/questions/7810563/how-do-you-use-settitletextattributesforstate-in-uibaritem-in-ios-5-0 –

Respuesta

7

Otro método consiste en: -

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; 
[button setBackgroundImage:[UIImage imageNamed:@"delete.png"] forState:UIControlStateNormal]; 
[button setTitle:@"Delete" forState:UIControlStateNormal]; 
button.titleLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:12.0f]; 
[button.layer setCornerRadius:4.0f]; 
[button.layer setMasksToBounds:YES]; 
[button.layer setBorderWidth:1.0f]; 
[button.layer setBorderColor: [[UIColor grayColor] CGColor]]; 
button.frame=CGRectMake(0.0, 100.0, 60.0, 30.0); 
[button addTarget:self action:@selector(batchDelete) forControlEvents:UIControlEventTouchUpInside]; 

UIBarButtonItem* deleteItem = [[UIBarButtonItem alloc] initWithCustomView:button]; 
97

mira esto: -

UIBarButtonItem *cancel = [[UIBarButtonItem alloc] initWithTitle:@"Title" style:UIBarButtonItemStyleBordered target:nil action:nil]; 
[cancel setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys: [UIColor redColor], UITextAttributeTextColor,nil] forState:UIControlStateNormal]; 
+0

provoca un bloqueo: Subproceso 1: EXC_BAD_ACESS (código = 2 dirección = 0x0) – user1645721

+1

+1 funciona perfecto .gracias. @ Usuario1645721 función setTitleTextAttributes disponible desde IOS 5 –

+1

Esto es mucho más fácil de implementar que la solución aceptada y funciona ... – c0d3Junk13

4

este código se utiliza para el cambio del color del texto de la UIBarButtonItem en la barra de navegación:

UILabel *lblTotCaratteri = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 50, 25)]; 
lblTotCaratteri.textAlignment = UITextAlignmentCenter; 
lblTotCaratteri.font = [UIFont italicSystemFontOfSize:13.0]; 
lblTotCaratteri.textColor = [UIColor redColor]; 
lblTotCaratteri.backgroundColor = [UIColor clearColor]; 
lblTotCaratteri.adjustsFontSizeToFitWidth = YES; 
lblTotCaratteri.text = @"Cancel"; 

UIBarButtonItem *lblCaratteri = [[UIBarButtonItem alloc] initWithCustomView: lblTotCaratteri]; 

self.navigationItem.rightBarButtonItem = lblCaratteri; 
10
UITextAttributeTextColor //Is deprecated on iOS 7. 

Este código es nosotros ed para cambiar el color del texto desde el proxy de apariencia.

[[UIBarButtonItem appearance] setTintColor:[UIColor redColor]]; 
+1

Esto establece el color de fondo, pidió color de texto. – Lukasz

+0

Utilice NSForegroundColorAttributeName en lugar de UITextAttributeTextColor – eXhausted

0

UITextAttributeTextColor // está en desuso en iOS 7.

Establecer el color de BarButtonItem de una manera como esto

[_barButtonItem setTitleTextAttributes: 
        [NSDictionary dictionaryWithObjectsAndKeys: 
          [UIColor colorWithRed:250/255.0 
              green:240/255.0 
              blue:230/255.0 
              alpha:1.0], 
          NSForegroundColorAttributeName,nil] 
        forState:UIControlStateNormal]; 
27

sólo una actualización iOS7 con Modern Obj-C Sintaxis:

[barButtonItem setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor redColor]} forState:UIControlStateNormal]; 
2

Pregunta anterior, aquí está la solución rápida 2.2:

let cancel = UIBarButtonItem(title: "CANCEL", style: .Bordered, target: self, action: #selector(goToPreviousView)) 
    cancel.setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.redColor()], forState: .Normal) 
    self.navigationItem.leftBarButtonItem = cancel 
0

Lo más importante que todo el mundo debería hacer si no es su proyecto y sólo tiene que añadir algunos cambios - es comprobar

[UIBarButtonItem appearance] 

perdí un montón de tiempo para darse cuenta de que alguien determinar la apariencia errónea de UIBarButtonItem

0

Aquí se actualiza rápido código de la versión 4.0:

let reset = UIBarButtonItem(title: "Reset All", style: .plain , target: self, action: #selector(self.resetButtonClicked(_ :))) 
      reset.setTitleTextAttributes([NSAttributedStringKey.foregroundColor: UIColor.red], for: .normal) 
Cuestiones relacionadas