Pregunta

Quiero cambiar el color del texto UILabel pero no puedo cambiar el color, Así es como mi código es el siguiente.

UILabel *categoryTitle = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 46, 16)];
categoryTitle.text = @"abc";
categoryTitle.backgroundColor = [UIColor clearColor];
categoryTitle.font = [UIFont systemFontOfSize:12];
categoryTitle.textAlignment = UITextAlignmentCenter;
categoryTitle.adjustsFontSizeToFitWidth = YES;
categoryTitle.textColor = [UIColor colorWithRed:188 green:149 blue:88 alpha:1.0];
[self.view addSubview:categoryTitle];
[categoryTitle release];

El color de texto de la etiqueta es de color blanco, no es mi color personalizado.

Gracias por cualquier ayuda.

¿Fue útil?

Solución

componentes RGB de UIColor se escalan entre 0 y 1, no hasta 255.

Trate

categoryTitle.textColor = [UIColor colorWithRed:(188/255.f) green:... blue:... alpha:1.0];

En Swift:

categoryTitle.textColor = UIColor(red: 188/255.0, green: ..., blue: ..., alpha: 1)

Otros consejos

Puede ser la mejor forma de hacerlo es

UIColor *color = [UIColor greenColor];
[self.myLabel setTextColor:color];

texto lo tanto hemos de color

Trate de éste, donde alfa es la opacidad y otros es rojo, verde, azul canales-

self.statusTextLabel.textColor = [UIColor colorWithRed:(233/255.f) green:(138/255.f) blue:(36/255.f) alpha:1];

Es posible, que no están conectados en InterfaceBuilder.

colour(colorWithRed:(188/255) green:(149/255) blue:(88/255)) texto es correcta, puede ser error en las conexiones,

backgroundColor se utiliza para el color de fondo de la etiqueta y el color de texto se utiliza para TextColor propiedad.

Añadir color del texto atribuido en el código SWIFT.

Swift 4:

  let greenColor = UIColor(red: 10/255, green: 190/255, blue: 50/255, alpha: 1)
  let attributedStringColor = [NSAttributedStringKey.foregroundColor : greenColor];

  let attributedString = NSAttributedString(string: "Hello World!", attributes: attributedStringColor)
  label.attributedText = attributedString

para Swift 3:

  let greenColor = UIColor(red: 10/255, green: 190/255, blue: 50/255, alpha: 1)
  let attributedStringColor : NSDictionary = [NSForegroundColorAttributeName : greenColor];


  let attributedString = NSAttributedString(string: "Hello World!", attributes: attributedStringColor as? [String : AnyObject])
  label.attributedText = attributedString 
// This is wrong 
categoryTitle.textColor = [UIColor colorWithRed:188 green:149 blue:88 alpha:1.0];

// This should be  
categoryTitle.textColor = [UIColor colorWithRed:188/255 green:149/255 blue:88/255 alpha:1.0];

// In the documentation, the limit of the parameters are mentioned.

colorWithRed: verde: azul: alpha: documentación enlace

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top