Ios 为我的应用程序创建夜间主题
我是iOS开发新手,想知道如何添加一个类似于TweetBot3和clear的夜间主题。从我的研究中,我还没有找到任何关于iOS应用程序主题的东西 我会在另一个故事板中为主题重新制作应用程序吗Ios 为我的应用程序创建夜间主题,ios,iphone,objective-c,xcode,ios7,Ios,Iphone,Objective C,Xcode,Ios7,我是iOS开发新手,想知道如何添加一个类似于TweetBot3和clear的夜间主题。从我的研究中,我还没有找到任何关于iOS应用程序主题的东西 我会在另一个故事板中为主题重新制作应用程序吗 谢谢。为了补充其他人所说的内容,这里有一个WWDC视频,介绍如何为您的代码设置主题。我更进一步,创建了一个轻量级的主题框架,我在我的几个应用程序中使用了这个框架。要点如下 每次创建标签、按钮等时(或者当它们将出现在屏幕上时,如果使用interface builder),都会将它们传递给设置其外观的主题实例。
谢谢。为了补充其他人所说的内容,这里有一个WWDC视频,介绍如何为您的代码设置主题。我更进一步,创建了一个轻量级的主题框架,我在我的几个应用程序中使用了这个框架。要点如下 每次创建标签、按钮等时(或者当它们将出现在屏幕上时,如果使用interface builder),都会将它们传递给设置其外观的主题实例。如果多个UI组件协同工作,请使用Facade设计模式将它们组合成一个对象(例如,如果您有一个按钮在特定位置具有客户包装、标签和图像,请将它们包装成一个名为(例如,为了方便起见)WrappedButton的类) 我有时发现用uml进行通信更容易,所以 主题协议可以是这样的
@protocol Theme <NSObject>
- (void)themeHeadingLabel:(UILabel *)headingLabel;
- (void)themeBodyLabel:(UILabel *)bodyLabel;
- (void)themeNavigationButton:(UIButton *)navigationButton;
- (void)themeActionButton:(UIButton *)actionButton;
@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
#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
当然,您将有一个或多个该协议的实现。这就是你的主题所在。下面是一些可能看起来像什么的片段
#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
我总是将其包装在主题管理器中,以帮助我跟踪主题。看起来像这样
@protocol Theme <NSObject>
- (void)themeHeadingLabel:(UILabel *)headingLabel;
- (void)themeBodyLabel:(UILabel *)bodyLabel;
- (void)themeNavigationButton:(UIButton *)navigationButton;
- (void)themeActionButton:(UIButton *)actionButton;
@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
#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 *)buildLabelWith:(NSString *)text
{
UILabel* headingLabel = [[UILabel alloc] init];
headingLabel.text = text;
[[ThemeManager theme] themeHeadingLabel:myHeading];
return headingLabel;
}
希望有帮助。如果您有任何问题,请告诉我。查看以下链接: