How to I properly set UIColor from int?
Asked Answered
L

7

6

I am trying to set the textColor of a UITextView by assigning it a value.

Earlier in the program I have

textView.textColor = 0x000000;

but later, when I have

textView.textColor = 0x888888;

a fatal error pops up saying: "Implicit conversion of 'int' to 'UIColor *' is disallowed with ARC".

How do I convert my int to a UIColor to properly set the text color? Why did it work with 0x000000 and not 0x888888?

Loseff answered 16/10, 2013 at 13:48 Comment(2)
check this:- #6207829Capstan
0x000000 is hexadecimal, this is a 16 base number system, but this is also considered the nil reference which is seen as 0 or 1 depending on if the reference exist or not so textView.textColor = 0x000000; would probably be black.Cribwork
C
12

as par you submited answer this is not an answer UIColorFromRGB is a Macro that define above at @implementation like

#define UIColorFromRGB(rgbValue) [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 green:((float)((rgbValue & 0xFF00) >> 8))/255.0 blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0];

Then you can use like

textView.textColor = UIColorFromRGB(0x888888);

you can use its property of UIColor for setting color of text bg-color etc in objective c

enter image description here

Capstan answered 16/10, 2013 at 14:4 Comment(0)
S
13

Swift version

You can use the following function to convert a RGB hexadecimal color Int to a UIColor.

func uiColorFromHex(rgbValue: Int) -> UIColor {
    
    // &  binary AND operator to zero out other color values
    // >>  bitwise right shift operator
    // Divide by 0xFF because UIColor takes CGFloats between 0.0 and 1.0
    
    let red =   CGFloat((rgbValue & 0xFF0000) >> 16) / 0xFF
    let green = CGFloat((rgbValue & 0x00FF00) >> 8) / 0xFF
    let blue =  CGFloat(rgbValue & 0x0000FF) / 0xFF
    let alpha = CGFloat(1.0)
    
    return UIColor(red: red, green: green, blue: blue, alpha: alpha)
}

And can be used like this

let myColorValue = 0x888888
let color = uiColorFromHex(myColorValue)
Sebastiansebastiano answered 11/9, 2015 at 8:16 Comment(0)
C
12

as par you submited answer this is not an answer UIColorFromRGB is a Macro that define above at @implementation like

#define UIColorFromRGB(rgbValue) [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 green:((float)((rgbValue & 0xFF00) >> 8))/255.0 blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0];

Then you can use like

textView.textColor = UIColorFromRGB(0x888888);

you can use its property of UIColor for setting color of text bg-color etc in objective c

enter image description here

Capstan answered 16/10, 2013 at 14:4 Comment(0)
U
1

it was necessary to use

textView.textColor = [UIColor colorWithRed:<#(CGFloat)#> green:<#(CGFloat)#> blue:<#(CGFloat)#> alpha:<#(CGFloat)#>];

try it:

- (UIColor *)UIColorFromRGB:(NSInteger)rgbValue {
    return [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0
                           green:((float)((rgbValue & 0xFF00) >> 8))/255.0
                            blue:((float)(rgbValue & 0xFF))/255.0
                           alpha:1.0];
}

...

textView.textColor = [self UIColorFromRGB:0x888888];
Ulla answered 16/10, 2013 at 13:59 Comment(0)
L
0

I figured out you can use UIColorFromRGB() to convert it to a UIColor.

e.g.

textView.textColor = UIColorFromRGB(0x888888);
Loseff answered 16/10, 2013 at 13:56 Comment(0)
B
0

You can do this by following these steps:

  1. Do convert hex value to RGB use HexToRGB.

  2. And then change the text color of your textview by doing this :

     textview.textColor = [UIColor colorWithRed:244/255.00f
        green:133/255.00f blue:116/255.00f alpha:1.0f]; // values are just an example
    
Bee answered 16/10, 2013 at 14:13 Comment(0)
C
0

Swift extension version

public extension UIColor {
public convenience init<T>(rgbValue: T, alpha: CGFloat = 1) where T: BinaryInteger {
    guard rgbValue > 0 else {
        self.init(red: 0, green: 0, blue: 0, alpha: alpha)
        return
    }

    guard rgbValue < 0xFFFFFF else {
        self.init(red: 1, green: 1, blue: 1, alpha: alpha)
        return
    }

    let r: CGFloat = CGFloat(((rgbValue & 0xFF0000) >> 16) / 0xFF)
    let g: CGFloat = CGFloat((rgbValue & 0x00FF00) >> 8 / 0xFF)
    let b: CGFloat = CGFloat((rgbValue & 0x0000FF) / 0xFF)

    self.init(red: r, green: g, blue: b, alpha: alpha)
}

}

usage:

let white =  UIColor(rgbValue: 16777215)
let red =    UIColor(rgbValue: 16711680)
let green =  UIColor(rgbValue: 0x00FF00)
let blue =   UIColor(rgbValue: 0x0000FF)
let yellow = UIColor(rgbValue: 0xFFFF00)
let black =  UIColor(rgbValue: 0)
Cockneyism answered 15/12, 2018 at 10:55 Comment(0)
S
0

When I was creating a Flutter plugin which needed this kind of conversion I had to modify a little the Suragch answer by including the alpha channel because in Flutter the alpha channel is already included in the hexadecimal color value:

static func uiColorFromHex(hexValue: Int) -> UIColor {
    let red =   CGFloat((hexValue & 0x00FF0000) >> 16)
    let green = CGFloat((hexValue & 0x0000FF00) >> 8)
    let blue =  CGFloat(hexValue & 0x000000FF)
    let alpha = CGFloat((hexValue & 0xFF000000) >> 24) / 0xFF

    return UIColor(red: red, green: green, blue: blue, alpha: alpha)
}
Saraisaraiya answered 25/9, 2022 at 11:2 Comment(1)
Aren't you missing / 0xFF at the end of the red, green, blue assignments?Guesstimate

© 2022 - 2024 — McMap. All rights reserved.