2015-06-27 2 views
54

Ho provato a cambiare i colori del testo per un pulsante, ma è ancora bianco.Come impostare il colore del testo del titolo di UIButton?

isbeauty = UIButton() 
isbeauty.setTitle("Buy", forState: UIControlState.Normal) 
isbeauty.titleLabel?.textColor = UIColorFromRGB("F21B3F") 
isbeauty.titleLabel!.font = UIFont(name: "AppleSDGothicNeo-Thin" , size: 25) 
isbeauty.backgroundColor = UIColor.clearColor() 
isbeauty.layer.cornerRadius = 5 
isbeauty.layer.borderWidth = 1 
isbeauty.layer.borderColor = UIColorFromRGB("F21B3F").CGColor 
isbeauty.frame = CGRectMake(300, 134, 55, 26) 
isbeauty.addTarget(self,action: "first:", forControlEvents: UIControlEvents.TouchUpInside) 
self.view.addSubview(isbeauty) 

Ho anche provato a cambiarlo il rosso, il nero, il blu, ma non succede nulla.

risposta

120

È necessario utilizzare func setTitleColor(_ color: UIColor?, forState state: UIControlState) nello stesso modo in cui si imposta il testo del titolo effettivo. Docs

isbeauty.setTitleColor(UIColorFromRGB("F21B3F"), forState: .Normal) 
+0

per amor leggibilità preferisco non qualificare la mia enumerazioni, ma questo è solo me – styler1972

+0

@ styler1972 preferisco che anche - a quanto pare ero inconsapevole di ciò quando inizialmente ho scritto la risposta – luk2302

+0

isbeauty.setTitleColor (UIColorFromRGB (rgbValue: "F21B3F"), per: .Normal) – Das

50

Swift 3, Swift 4

per migliorare commenti. Questo dovrebbe funzionare:

button.setTitleColor(.red, for: .normal) 
3

esempio nella creazione titolo tasto colorato

btnDone.setTitleColor(UIColor.black, for: UIControlState.normal)