¿Cómo puedo crear un UIColor desde RGBA?


Quiero usar NSAttributedString en mi proyecto, pero cuando estoy tratando de establecer el color, que no es del conjunto estándar(redColor, blackColor, greenColor etc.) UILabel muestra estas letras en color blanco. Aquí está mi línea de este código.

[attributedString addAttribute:NSForegroundColorAttributeName
                         value:[UIColor colorWithRed:66
                                               green:79
                                                blue:91
                                               alpha:1]
                         range:NSMakeRange(0, attributedString.length)];

Traté de hacer color con CIColor desde Core Image framework, pero mostró los mismos resultados. ¿Qué debo cambiar en mi código para realizarlo de la manera correcta?

Gracias por las respuestas, chicos!

Author: E.A. Wilson, 2012-11-05

5 answers

Sus valores son incorrectos, debe dividir cada valor de color por 255.0.

[UIColor colorWithRed:66.0f/255.0f
                green:79.0f/255.0f
                 blue:91.0f/255.0f
                alpha:1.0f];

Los documentos dicen:

+ (UIColor *)colorWithRed:(CGFloat)red
                    green:(CGFloat)green
                     blue:(CGFloat)blue
                    alpha:(CGFloat)alpha

Parámetros

Rojo El componente rojo del objeto color, especificado como un valor de 0.0 a 1.0.

Verde El componente verde del objeto color, especificado como un valor de 0.0 a 1.0.

Azul El componente azul del objeto color, especificado como un valor de 0.0 a 1.0.

Alfa El valor de opacidad del objeto color, especificado como un valor de 0.0 a 1.0.

Referencia aquí.

 103
Author: yfrancis,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-05-25 08:41:04

Una de mis macros favoritas, ningún proyecto sin:

#define RGB(r, g, b) [UIColor colorWithRed:(float)r / 255.0 green:(float)g / 255.0 blue:(float)b / 255.0 alpha:1.0]
#define RGBA(r, g, b, a) [UIColor colorWithRed:(float)r / 255.0 green:(float)g / 255.0 blue:(float)b / 255.0 alpha:a]

Usando como:

[attributedString addAttribute:NSForegroundColorAttributeName
                         value:RGB(66, 79, 91)
                         range:NSMakeRange(0, attributedString.length)];
 25
Author: Geri,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-01-23 00:52:07

UIColor utiliza un rango de 0 a 1.0, no enteros a 255.. Prueba esto:

// create color
UIColor *color = [UIColor colorWithRed:66/255.0
                                 green:79/255.0
                                  blue:91/255.0
                                 alpha:1];

// use in attributed string
[attributedString addAttribute:NSForegroundColorAttributeName
                         value:color
                         range:NSMakeRange(0, attributedString.length)];
 5
Author: calimarkus,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-05-25 13:51:33

Por favor, pruebe el código

[attributedString addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithRed:77.0/255.0f green:104.0/255.0f blue:159.0/255.0f alpha:1.0] range:NSMakeRange(0, attributedString.length)];

Como

Label.textColor=[UIColor colorWithRed:77.0/255.0f green:104.0/255.0f blue:159.0/255.0f alpha:1.0];  

Los componentes RGB de UIColor se escalan entre 0 y 1, no hasta 255.

 3
Author: SameSung Vs Iphone,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-11-07 16:01:24

Desde que @ Jaswanth Kumar preguntó, aquí está la Swift versión de LSwift :

extension UIColor {
    convenience init(rgb:UInt, alpha:CGFloat = 1.0) {
        self.init(
            red: CGFloat((rgb & 0xFF0000) >> 16) / 255.0,
            green: CGFloat((rgb & 0x00FF00) >> 8) / 255.0,
            blue: CGFloat(rgb & 0x0000FF) / 255.0,
            alpha: CGFloat(alpha)
        )
    }
}

Uso: let color = UIColor(rgb: 0x112233)

 3
Author: superarts.org,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-09-09 13:54:16