I have 2 buttons (button-A and button-B). When I click button-A, I want to change the text color of Button-B but I want to give it in a hexadecimal value. I have seen a lot of links for hexadecimal value font color but I am unable to figure out how to use. I want to change the text font color of button to value (#191f2d). In swift iOS. Please help me out
Asked
Active
Viewed 839 times
2 Answers
1
In Appdelegate file after import statement add this
extension UIColor {
// Usage: UIColor(hex: 0xFC0ACE)
convenience init(hex: Int) {
self.init(hex: hex, alpha: 1)
}
// Usage: UIColor(hex: 0xFC0ACE, alpha: 0.25)
convenience init(hex: Int, alpha: Double) {
self.init(
red: CGFloat((hex >> 16) & 0xff) / 255,
green: CGFloat((hex >> 8) & 0xff) / 255,
blue: CGFloat(hex & 0xff) / 255,
alpha: CGFloat(alpha))
}
}
and wherever you want to use it just use it like this
button.setTitleColor(UIColor.init(hex: 0x191f2d), forState: UIControlState.Normal)
use 0x before Hex value instead of #.
Hope it will help You.

JigneshP
- 344
- 2
- 7
-
Thanks a lot @JigneshP it was a great solution! :) – Muneeb Rehman Jun 22 '16 at 09:26
0
You need an extension of UIColor
, that takes the hex value and creates an UIImage
. There's a lot of extensions out there, a simple Google search finds many results...
I personally use this: https://github.com/thii/SwiftHEXColors
Just follow the instructions or drag and drop SwiftHEXColors.swift
file to your project.

Dejan Skledar
- 11,280
- 7
- 44
- 70