Voglio cambiare il colore del testo UILabel ma non posso cambiare il colore, Ecco come appare il mio codice.
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];
Il colore del testo dell’etichetta è bianco, non è il mio colore personalizzato.
Grazie per l’aiuto.
I componenti RGB di UIColor sono scalati tra 0 e 1, non fino a 255.
Provare
categoryTitle.textColor = [UIColor colorWithRed:(188/255.f) green:... blue:... alpha:1.0];
In Swift:
categoryTitle.textColor = UIColor(red: 188/255.0, green: ..., blue: ..., alpha: 1)
Potrebbe essere il modo migliore è
UIColor *color = [UIColor greenColor]; [self.myLabel setTextColor:color];
Quindi abbiamo un testo colorato
Prova questo, dove l’alfa è l’opacità e gli altri sono il rosso, il verde, il blu-
self.statusTextLabel.textColor = [UIColor colorWithRed:(233/255.f) green:(138/255.f) blue:(36/255.f) alpha:1];
È ansible, non sono collegati in InterfaceBuilder.
Colore del testo (colorWithRed:(188/255) green:(149/255) blue:(88/255))
è corretto, potrebbe essere un errore nelle connessioni,
backgroundcolor è usato per il colore di sfondo dell’etichetta e textcolor è usato per la proprietà textcolor.
Aggiungi il colore del testo attribuito nel codice rapido.
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
per 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: blu: alpha: collegamento alla documentazione