Actualización: Recomendaría comenzar por comprender UIAppearance
API, y ver qué tan bien se adaptan a sus necesidades. UIAppearance
es una forma conveniente de proporcionar una estilización predeterminada personalizada de los atributos de controles específicos en múltiples niveles (por ejemplo, global o contextualmente).
Mi respuesta original, que es anterior a UIAppearance
's disponibilidad:
ya que estamos trabajando con un lenguaje basado en objetos ...
para la ejecución, que depende sobre cómo quieres que se comporte/ejecute. cuando la implementación no sea trivial, a menudo crearé un protocolo. usted podría utilizar métodos de clase o métodos de instancia y de manera significativa optimizar estos tipos para su uso porque se crea un menor número de colores intermedios, fuentes, imágenes, etc.
una interfaz básica podría tomar la forma:
@protocol MONLabelThemeProtocol
- (UIFont *)labelFont;
- (UIColor *)labelTextColor;
- (UITextAlignment)labelTextAlignment;
// ...
@end
@protocol MONTableViewCellThemeProtocol
- (UIFont *)tableViewCellFont;
- (UIColor *)tableViewCellTextColor;
- (UIImage *)tableViewCellImage;
- (NSInteger)tableViewCellIndentationLevel;
- (CGFloat)tableViewCellIndentationWidth;
// ...
@end
luego una tema amalgamate simple podría ser declarado como esto:
@interface MONAmalgamateThemeBase : NSObject
< MONLabelThemeProtocol, MONTableViewCellThemeProtocol >
{
@protected
/* labels */
UIFont * labelFont;
UIColor * labelTextColor;
UITextAlignment labelTextAlignment;
// ...
/* table view cells */
UIFont * tableViewCellFont;
UIColor * tableViewCellTextColor;
UIImage * tableViewCellImage;
NSInteger tableViewCellIndentationLevel;
CGWidth tableViewCellIndentationWidth;
// ...
}
@end
en este ejemplo, el amalgamate define los captadores y dealloc y espera las subclases para inicializar las variables de instancia. también podría admitir la inicialización lenta si los tiempos de inicialización son altos (por ejemplo, usa muchas imágenes).
entonces una especialización podría tomar la forma:
@interface MONDarkTheme : MONAmalgamateThemeBase
@end
@implementation MONDarkTheme
- (id)init
{
self = [super init];
if (nil != self) {
labelFont = [[UIFont boldSystemFontOfSize:15] retain];
labelTextColor = [[UIColor redColor] retain];
// and so on...
}
return self;
}
// ...
@end
/* declare another theme and set it up appropriately */
@interface MONLightTheme : MONAmalgamateThemeBase
@end
a continuación, sólo reutilizar los casos tema (por ejemplo MONDarkTheme) a lo largo de la aplicación para estilizar las vistas. si tiene muchos temas o no son triviales para construir, entonces puede querer crear una colección para temas (administrador de temas). el amalgama también podría tomar un parámetro, como init con theme si tus necesidades son simples. incluso puede configurar objetos para registrar cambios en los temas, si necesita soporte para cambios dinámicos.
por último, puede crear un simple aplicador tema para hacer la vida más fácil - de este modo:
@interface UILabel (MONThemeAdditions)
- (void)mon_applyMONLabelTheme:(id<MONLabelTheme>)theme;
@end
@implementation UILabel (MONThemeAdditions)
- (void)mon_applyMONLabelTheme:(id<MONLabelTheme>)theme
{
assert(theme);
if (nil == theme) return;
self.font = [theme labelFont];
self.textColor = [theme labelTextColor];
self.textAlignment = [theme labelTextAlignment];
}
@end
Es posible que tenga que mirar esta biblioteca. Admite múltiples temas/máscaras sobre la marcha. Admite imágenes y colores actualmente. El soporte de fuentes se agregará en el futuro. https://github.com/charithnidarsha/MultiThemeManager –