Yog*_*wal 23 xcode objective-c ios
设计iOS应用程序的推荐方法是什么?例如,如果有多个标签或文本视图,如何在一个地方更新字体样式/颜色更新所有其他位置的样式/颜色?
我知道子分类可能是一种方式......还有其他方法吗?
您可以将标准头文件导入到所有控制器中,并为样式设置几个常量...示例:
Styles.h
#define kFontSize 14
#define kFontFamily @"Helevetica"
Run Code Online (Sandbox Code Playgroud)
调节器
#import "Styles.h" // at the top
myLabel.font = [UIFont fontWithName:kFontFamily size:kFontSize];
Run Code Online (Sandbox Code Playgroud)
我个人认为Interface Builder是最好的样式方式,但是这可以直接回答你的问题.
更新:我建议首先了解UIAppearance
API,看看它们是否适合您的需求.UIAppearance
是一种在多个级别(例如全局或上下文)提供特定控件属性的自定义默认样式的便捷方式.
我原来的答案,早于UIAppearance
可用性:
因为我们正在使用基于对象的语言......
对于实现,它取决于您希望它的行为/执行方式.当实现变得非常重要时,我会经常创建一个协议.您可以使用类方法或实例方法并显着优化这些类型以供您使用,因为您创建的中间颜色,字体,图像等较少.
一个基本的界面可以采取以下形式:
@protocol MONLabelThemeProtocol
- (UIFont *)labelFont;
- (UIColor *)labelTextColor;
- (UITextAlignment)labelTextAlignment;
// ...
@end
@protocol MONTableViewCellThemeProtocol
- (UIFont *)tableViewCellFont;
- (UIColor *)tableViewCellTextColor;
- (UIImage *)tableViewCellImage;
- (NSInteger)tableViewCellIndentationLevel;
- (CGFloat)tableViewCellIndentationWidth;
// ...
@end
Run Code Online (Sandbox Code Playgroud)
然后可以像这样声明一个简单的合并主题:
@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
Run Code Online (Sandbox Code Playgroud)
在此示例中,amalgamate定义getter和dealloc,并期望子类初始化实例变量.如果初始化时间很长(例如,使用许多图像),您还可以支持延迟初始化.
那么专业化可以采取以下形式:
@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
Run Code Online (Sandbox Code Playgroud)
然后只需在整个应用程序中重用主题实例(例如MONDarkTheme)来设置视图的样式.如果你有很多主题或者它们不是很容易构建,那么你可能想要为主题创建一个集合(主题管理器).如果你的需求很简单,amalgamate也可以带一个参数,比如带主题的init.如果需要动态更改支持,甚至可以配置对象以注册主题更改.
最后,你可以创建一个简单的主题应用程序,让生活更轻松 - 就像这样:
@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
Run Code Online (Sandbox Code Playgroud)
坦率地说,最好的方法是使用Interface Builder.虽然在代码中的某个地方更改单个常量并让整个应用程序更改样式似乎很不错,但它从来都不会那样.以下是我的推理:
1)开发人员不像接口构建器那样编写接口代码. 接口生成器是被提炼,测试,并应允在工具多年.它提供字体,文本对齐,阴影等.它可以向后兼容,可以追溯到你想要的地方.它为任何数量的开发人员和设计人员提供了一种非常简单的方法,可以非常直接地进入并开展工作.
2)您总是需要考虑边缘情况. 当然,一个简单的常数会做你想要的最多的时间,但你最终必须在这里破解一些东西并在那里潜行.您开始编写的"简单"界面代码将会增长,增长和增长.其他开发人员必须维护该代码.您将不得不维护该代码.你将不得不提交和修复错误,调整它,除了等等.它将不可避免地成为一堆乱七八糟的东西.
3)你写的代码越多,你写的错误就越多. 界面构建器用于构建大多数iOS应用程序的"外观".用它.不要太聪明.
注意:我知道"界面"构建器无法为所有应用程序执行所有操作.有些情况下编码接口是唯一的解决方案.这个答案只是我在大部分应用中使用的一般"最佳实践".
归档时间: |
|
查看次数: |
8698 次 |
最近记录: |