Objetivo C definición de constantes de UIColor


Tengo una aplicación para iPhone con algunos colores personalizados definidos para mi tema. Dado que estos colores se arreglarán para mi interfaz de usuario, me gustaría definir los colores en una clase a incluir (Constantes.h y Constantes.m). ¿Cómo hago eso? (Simplemente definirlos no funciona porque los UICOLORES son mutables, y causaría errores - Initalizer no constante).

/* Constants.h */
extern UIColor *test;

/* Constants.m */
UIColor *test = [UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:1.0];

Gracias!

Author: futureelite7, 2010-05-13

7 answers

Un UIColor no es mutable. Normalmente hago esto con colores, fuentes e imágenes. Puedes modificarlo fácilmente para usar singletons o tener un inicializador estático.

@interface UIColor (MyProject)

+(UIColor *) colorForSomePurpose;

@end

@implementation UIColor (MyProject)

+(UIColor *) colorForSomePurpose { return [UIColor colorWithRed:0.6 green:0.8 blue:1.0 alpha:1.0]; }

@end
 42
Author: drawnonward,
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
2010-05-13 04:18:01

Por simplicidad hice esto:

/* Constants.h */
#define myColor [UIColor colorWithRed:255.0/255.0 green:255.0/255.0 blue:255.0/255.0 alpha:1.0]

No olvides omitir el ';' para que puedas usarlo como una expresión normal.

No estoy seguro de si hay algo técnicamente incorrecto con este enfoque, pero funciona bien, y evita el error del inicializador constante en tiempo de compilación-este código está efectivamente atascado en cualquier lugar que ponga 'MyColor', por lo que nunca se compila hasta que realmente lo use.

 30
Author: Fateh Khalsa,
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
2013-05-01 13:29:56

Otra opción

En su .h usted puede hacer

extern UIColor *  const COLOR_LIGHT_BLUE;

En su. mm usted puede hacer

UIColor* const COLOR_LIGHT_BLUE = [[UIColor alloc] initWithRed:21.0f/255 green:180.0f/255  blue:1 alpha:1];//;#15B4FF
 13
Author: yeahdixon,
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-01-06 15:38:20

Si estás buscando uno rápido y sucio sin extensiones, ve con clang :

#define kGreenColor colorWithRed:(0/255.0) green:(213/255.0) blue:(90/255.0) alpha:1.0

- (void)doSomething
{
   _label.textColor = [UIColor kGreenColor];

}
 3
Author: AmitP,
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
2016-07-01 06:59:43

A menudo las personas ponen constantes globales en objetos singleton - o como drawnonward señaló, puede hacerlas accesibles a través de un método de clase de alguna clase.

 0
Author: Kendall Helmstetter Gelner,
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
2010-05-13 04:20:51

Aquí hay otra manera:

Cabecera:

#if !defined(COLORS_EXTERN)
    #define COLORS_EXTERN extern
#endif

COLORS_EXTERN UIColor *aGlobalColor;

Aplicación:

#define COLORS_EXTERN
#import "GlobalColors.h"


@interface GlobalColors : NSObject
@end

@implementation GlobalColors

+ (void)load
{
    aGlobalColor = [UIColor colorWithRed:0.2 green:0.3 blue:0.4 alpha:1];
}

@end

Es un poco un truco, pero no necesita redefinir el color en la implementación y puede acceder a los colores sin una llamada al método.

 0
Author: goetz,
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-12-04 10:14:30

Use AppController para hacer que los colores sean accesibles globalmente, en lugar de una variable estática. De esa manera tiene sentido desde el punto de vista de la arquitectura, y también si desea cambiar hipotéticamente los esquemas de color, incluso mientras se ejecuta, esto solo sería un método o dos en el AppController

 -1
Author: Jared Pochtar,
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
2010-05-13 04:25:03