【发布时间】:2014-01-08 14:03:45
【问题描述】:
我是 iOS 开发新手,想知道如何添加类似于 tweetbot 3 和 clear 的夜间主题。根据我的研究,我还没有真正找到关于主题 iOS 应用程序的任何内容。
我是否可以在另一个主题特定的情节提要中重新制作应用程序?
谢谢。
【问题讨论】:
标签: ios iphone objective-c xcode ios7
我是 iOS 开发新手,想知道如何添加类似于 tweetbot 3 和 clear 的夜间主题。根据我的研究,我还没有真正找到关于主题 iOS 应用程序的任何内容。
我是否可以在另一个主题特定的情节提要中重新制作应用程序?
谢谢。
【问题讨论】:
标签: ios iphone objective-c xcode ios7
为了补充其他人所说的内容,有一个关于主题化代码的 WWDC 视频。我更进一步,为我在几个应用程序中使用的主题创建了一个轻量级框架。要点如下。
每次您创建标签、按钮等时(或者当它们将出现在屏幕上时,如果您使用界面生成器),您将它们传递给设置其外观和感觉的主题实例。如果多个 UI 组件一起工作,请使用 Facade 设计模式将它们组合成一个对象(例如,如果您有一个在特定位置具有客户包装器、标签和图像的按钮,请将它们包装到一个单独的类——例如——WrappedButton)。
我有时发现在 uml 中交流更容易,所以...
主题协议可能看起来像这样。
@protocol Theme <NSObject>
- (void)themeHeadingLabel:(UILabel *)headingLabel;
- (void)themeBodyLabel:(UILabel *)bodyLabel;
- (void)themeNavigationButton:(UIButton *)navigationButton;
- (void)themeActionButton:(UIButton *)actionButton;
@end
顺便说一句,我通常将代码放在那里,以允许按钮、标签等响应 iOS7 中的文本大小更改(来自设置应用程序)。所以也可能有类似的方法,
- (void)respondToTextSizeChangeForHeadingLabel:(UILabel *)headingLabel;
- (void)respondToTextSizeChangeForBodyLabel:(UILabel *)bodyLabel;
// and do the same for buttons
当然,您将拥有一个或多个该协议的实现。这是您的主题所在的地方。以下是一些可能的样子。
#import "Theme.h"
@interface LightTheme : NSObject <Theme>
@end
@implementation LightTheme
- (void)themeHeadingLabel:(UILabel *)headingLabel
{
headingLabel.backgroundColor = [UIColor lightTextColor];
headingLabel.textColor = [UIColor darkTextColor];
headingLabel.font = [UIFont preferredFontForTextStyle:UIFontTextStyleHeadline];
}
// the rest of your theming
@end
您可以有一个黑暗主题,其实现看起来像这样。
@implementation DarkTheme
- (void)themeHeadingLabel:(UILabel *)headingLabel
{
headingLabel.backgroundColor = [UIColor darkGrayColor];
headingLabel.textColor = [UIColor lightTextColor];
headingLabel.font = [UIFont preferredFontForTextStyle:UIFontTextStyleHeadline];
}
// the rest of your theming
@end
我总是将其包装在 ThemeManager 中以帮助我跟踪主题。可能看起来像这样。
#import "Theme.h"
@interface ThemeManager : NSObject
+ (id <Theme>)theme;
@end
#import "LightTheme.h"
#import "DarkTheme.h"
@implementation ThemeManager
+ (id <Theme>)theme
{
// here you'd return either your light or dark theme, depending on your program's logic
}
@end
现在,要将它们结合在一起,您可以直接使用或在工厂中使用。
UILabel* headingLabel = [[UILabel alloc] init];
headingLabel.text = @"My Heading";
[[ThemeManager theme] themeHeadingLabel:myHeading];
// use the label
或者作为工厂,实现看起来像这样。
- (UILabel *)buildLabelWith:(NSString *)text
{
UILabel* headingLabel = [[UILabel alloc] init];
headingLabel.text = text;
[[ThemeManager theme] themeHeadingLabel:myHeading];
return headingLabel;
}
希望对您有所帮助。如果您有任何问题,请告诉我。
【讨论】: