加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ios – 为我的应用创建夜间主题

发布时间:2020-12-14 18:56:21 所属栏目:百科 来源:网络整理
导读:我是iOS开发的新手,想知道如何添加类似于tweetbot 3的夜间主题并清除.根据我的研究,我还没有找到关于iOS应用程序的任何内容. 我会在另一个专门针对主题的故事板中重新制作应用吗? 谢谢. 解决方法 为了添加其他人所说的内容,有一个关于代码的WWDC视频.我已经
我是iOS开发的新手,想知道如何添加类似于tweetbot 3的夜间主题并清除.根据我的研究,我还没有找到关于iOS应用程序的任何内容.

我会在另一个专门针对主题的故事板中重新制作应用吗?

谢谢.

解决方法

为了添加其他人所说的内容,有一个关于代码的WWDC视频.我已经采取了更进一步的步骤,为我在几个应用程序中使用的主题创建了一个轻量级框架.要点如下.

每次创建标签,按钮等(或者当它们将出现在屏幕上时,如果您使用界面构建器),您将它们传递给设置其外观的Theme实例.如果多个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;
}

希望有所帮助.如果您有任何疑问,请告诉我.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读