6rfitq
Last Updated: October 28, 2018
·
14.29K
· itseranga

IOS UI colors with hex values in swfit

UIColorFromHex function

  • There is no any default function to define UI colors with hex values in IOS

  • Following swift function can be use to define colors with hex(RBG) values

func UIColorFromHex(rgbValue:UInt32, alpha:Double=1.0)->UIColor {
    let red = CGFloat((rgbValue & 0xFF0000) >> 16)/256.0
    let green = CGFloat((rgbValue & 0xFF00) >> 8)/256.0
    let blue = CGFloat(rgbValue & 0xFF)/256.0

    return UIColor(red:red, green:green, blue:blue, alpha:CGFloat(alpha))
}

// rbgValue - define hex color value
// alpha - define transparency value
// returns - CGColor

Function usage(set view background color)

var loadingView: UIView = UIView()
loadingView.frame = CGRectMake(0, 0, 80, 80)
loadingView.center = self.view.center

// set background color
loadingView.backgroundColor = UIColorFromHex(0x444444, alpha: 0.7)
  • Output would be like below

Picture

Function usage(set view border color)

  • When define border color of a view, need to convert UIColor to CGColor
var loadingView: UIView = UIView()
loadingView.frame = CGRectMake(0, 0, 80, 80)
loadingView.center = self.view.center

// set background color
loadingView.backgroundColor = UIColorFromHex(0x444444, alpha: 0.7)

// set border width and color
loadingView.layer.borderColor = UIColorFromHex(0xdf661d).CGColor
loadingView.layer.borderWidth = 2
  • Output would be like below

Picture

4 Responses
Add your response

30071

hello, here is how to use the hex colour values

ffffff are actually 3 color components in hexadecimal notation - red ff, green ff and blue ff. You can write hexadecimal notation in Swift using 0x prefix, e.g 0xFF

To simplify the conversion, let's create an initializer that takes integer (0 - 255) values:

extension UIColor {
convenience init(red: Int, green: Int, blue: Int) {
assert(red >= 0 && red <= 255, "Invalid red component")
assert(green >= 0 && green <= 255, "Invalid green component")
assert(blue >= 0 && blue <= 255, "Invalid blue component")

self.init(red: CGFloat(red) / 255.0, green: CGFloat(green) / 255.0, blue: CGFloat(blue) / 255.0, alpha: 1.0)

}

convenience init(rgb: Int) {
self.init(
red: (rgb >> 16) & 0xFF,
green: (rgb >> 8) & 0xFF,
blue: rgb & 0xFF
)
}
}
Usage:

let color = UIColor(red: 0xFF, green: 0xFF, blue: 0xFF)
let color2 = UIColor(rgb: 0xFFFFFF)
How to get alpha?

Depending on your use case, you can simply use the native UIColor.withAlphaComponent method, e.g.

let semitransparentBlack = UIColor(rgb: 0x000000).withAlphaComponent(0.5)
Or you can add an additional (optional) parameter to the above methods:

convenience init(red: Int, green: Int, blue: Int, a: CGFloat = 1.0) {
self.init(
red: CGFloat(red) / 255.0,
green: CGFloat(green) / 255.0,
blue: CGFloat(blue) / 255.0,
alpha: a
)
}

convenience init(rgb: Int, a: CGFloat = 1.0) {
self.init(
red: (rgb >> 16) & 0xFF,
green: (rgb >> 8) & 0xFF,
blue: rgb & 0xFF,
a: a
)
}
(we cannot name the parameter alpha because of a name collision with the existing initializer).

Called as:

let color = UIColor(red: 0xFF, green: 0xFF, blue: 0xFF, a: 0.5)
let color2 = UIColor(rgb: 0xFFFFFF, a: 0.5)
To get the alpha as an integer 0-255, we can

convenience init(red: Int, green: Int, blue: Int, a: Int = 0xFF) {
self.init(
red: CGFloat(red) / 255.0,
green: CGFloat(green) / 255.0,
blue: CGFloat(blue) / 255.0,
alpha: CGFloat(a) / 255.0
)
}

// let's suppose alpha is the first component (ARGB)
convenience init(argb: Int) {
self.init(
red: (argb >> 16) & 0xFF,
green: (argb >> 8) & 0xFF,
blue: argb & 0xFF,
a: (argb >> 24) & 0xFF
)
}
Called as

let color = UIColor(red: 0xFF, green: 0xFF, blue: 0xFF, a: 0xFF)
let color2 = UIColor(argb: 0xFFFFFFFF)
Or a combination of the previous methods. There is absolutely no need to use strings.

https://tutuappx.com/ https://textnow.vip/ https://photomath.vip/

6 months ago ·
30636

Thank you for sharing this article. Get mobdroapp from https://mobdroapp.one/ which lets you to watch unlimited free movies right from your android smartphone.

3 months ago ·
30895

How to use Hex color codes in CSS

Hex color codes are the most common method of adding color to an
HTML element using CSS. In your stylesheet, you can use the CSS color property to change the default color of your website's text.
CSSA second option is to include the CSS styles right in the

<head> of your HTML document using <style> tags, like below:
HTML
Easy, right? You can use the CSS color property with a Hex code

on just about any HTML element, the <body> tag is only one
example. Go crazy!

JoseMark;
http://movieboxhdapp.org/megabox-hd-apk https://emus4u.co https://tutuappapk.vip https://tutuhelperx.com

about 2 months ago ·
31083

To simplify the conversion, let's create an initializer that takes integer (0 - 255) values:

extension UIColor {
convenience init(red: Int, green: Int, blue: Int) {
assert(red >= 0 && red <= 255, "Invalid red component")
assert(green >= 0 && green <= 255, "Invalid green component")
assert(blue >= 0 && blue <= 255, "Invalid blue component")

self.init(red: CGFloat(red) / 255.0, green: CGFloat(green) / 255.0, blue: CGFloat(blue) / 255.0, alpha: 1.0)
}

convenience init(rgb: Int) {
self.init(
red: (rgb >> 16) & 0xFF,
green: (rgb >> 8) & 0xFF,
blue: rgb & 0xFF
)
}
}
Usage:

let color = UIColor(red: 0xFF, green: 0xFF, blue: 0xFF)
let color2 = UIColor(rgb: 0xFFFFFF)
https://dltutuapp.com/
https://9apps.ooo/
https://showbox.run/

about 1 month ago ·