156

I tried changing the colors of the text for a button, but it's still staying white.

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)

I also tried changing it the red, black, blue, but nothing is happening.

11 Answers 11

364

You have to use func setTitleColor(_ color: UIColor?, for state: UIControl.State) the same way you set the actual title text. Docs

isbeauty.setTitleColor(UIColorFromRGB("F21B3F"), for: .normal)
6
  • for readability sake i prefer not to qualify my enums, but thats just me
    – Tyler
    Commented May 19, 2016 at 21:57
  • @styler1972 I prefer that too - apparently I was unaware of that when I initially wrote the answer
    – luk2302
    Commented May 20, 2016 at 7:04
  • 1
    isbeauty.setTitleColor(UIColorFromRGB(rgbValue: "F21B3F"), for: .Normal) Commented Oct 27, 2017 at 18:58
  • 3
    I believe that as of xcode 9, it's now ".normal" instead of ".Normal" Commented Nov 17, 2017 at 17:21
  • 4
    my code: yourButtonName.setTitleColor(UIColor.red, for: .normal)
    – Dev Gurung
    Commented Jan 26, 2018 at 21:10
128

Swift UI solution

Button(action: {}) {
            Text("Button")
        }.foregroundColor(Color(red: 1.0, green: 0.0, blue: 0.0))

Swift 3, Swift 4, Swift 5

to improve comments. This should work:

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

Example in setting button title color

btnDone.setTitleColor(.black, for: .normal)
2
  • 13
    What's the difference between mine answer and yours?
    – Vyacheslav
    Commented Sep 22, 2018 at 10:50
  • 2
    it's obvious - color
    – janaz
    Commented Mar 9, 2021 at 18:17
4
func setTitleColor(_ color: UIColor?, 
               for state: UIControl.State)

Parameters:

color:
The color of the title to use for the specified state.

state:
The state that uses the specified color. The possible values are described in UIControl.State.

Sample:

let MyButton = UIButton()
MyButton.setTitle("Click Me..!", for: .normal)
MyButton.setTitleColor(.green, for: .normal)
2

This is swift 5 compatible answer. If you want to use one of the built-in colours then you can simply use

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

If you want some custom colours, then create an extension for a UIColor as below first.

import UIKit
extension UIColor {
    static var themeMoreButton = UIColor.init(red: 53/255, green: 150/255, blue: 36/255, alpha: 1)
}

Then use it for your button as below.

button.setTitleColor(UIColor.themeMoreButton, for: .normal)

Tip: You can use this method to store custom colours from rgba colour code and reuse it throughout your application.

2

if you are using a button without NSAttribute then you can simply set the title and color of button by using

yourButtonName.setTitle("Your Title", for: .normal)

enterCustomAmount.setTitleColor(.gray, for: .normal)

but if you give NSAttribute in your button property then you first need to set the attribute for the button

var myAttribute = [ NSAttributedString.Key.foregroundColor: UIColor.init(hexString: "#FFAEA9")]

or for more then one property you can use comma separator like

var myAttribute = [ NSAttributedString.Key.foregroundColor: UIColor.init(hexString: "#FFAEA9"), NSAttributedString.Key.font: UIFont(name: "Dubai-Medium", size: 16) ]

and assign it to your UIButton like this

 let myString = "Your title"
            let text = NSAttributedString(string: myString, attributes: myAttribute)
            enterCustomAmount.setAttributedTitle(text, for: .normal)
1

You can set UIButton title color with hex code

btn.setTitleColor(UIColor(hexString: "#95469F"), for: .normal)

Added this extension class to support string color code

public extension UIColor {
        convenience init(hexString: String, alpha: CGFloat = 1.0) {
            let hexString: String = hexString.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines)
            let scanner = Scanner(string: hexString)
            
            if (hexString.hasPrefix("#")) {
                scanner.currentIndex = .init(utf16Offset: 1, in: hexString)
            }
            
            var color: UInt64 = 0
            scanner.scanHexInt64(&color)
            let mask = 0x000000FF
            let r = Int(color >> 16) & mask
            let g = Int(color >> 8) & mask
            let b = Int(color) & mask
            let red   = CGFloat(r) / 255.0
            let green = CGFloat(g) / 255.0
            let blue  = CGFloat(b) / 255.0
            
            self.init(red:red, green:green, blue:blue, alpha:alpha)
        }
    }
2
  • UIColor does not provide a hexString: initializer.
    – HangarRash
    Commented Jun 5, 2023 at 1:04
  • This initializer is s extension function. Commented Jun 6, 2023 at 4:22
0

referring to radio buttons ,you can also do it with Segmented Control as following:

step 1: drag a segmented control to your view in the attribute inspector change the title of the two segments ,for example "Male" and "Female"

step 2: create an outlet & an action for it in the code

step 3: create a variable for future use to contain choice's data

in the code do as following:

@IBOutlet weak var genderSeg: UISegmentedControl!

var genderPick : String = ""


 @IBAction func segAction(_ sender: Any) {

    if genderSeg.selectedSegmentIndex == 0 {
         genderPick = "Male"
        print(genderPick)
    } else if genderSeg.selectedSegmentIndex == 1 {
        genderPick = "Female"
          print(genderPick)
    }
}
0

set title color

btnGere.setTitleColor(#colorLiteral(red: 0, green: 0, blue: 0, alpha: 1), for: .normal)
-1

To change color of text

button.titleLabel.textColor = UIColor.grayColor()

To change state, on button press add following -

button.enabled = true

IBAction method should be like -

@IBAction func buttonTapped(sender : UIButton!) {
    sender.enabled = false
}
-1

continueButton.tintColor = UIColor.white

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.