DKNightVersion 2.4.3

DKNightVersion 2.4.3

测试已测试
语言语言 Obj-CObjective C
许可 MIT
发布最后发布2017 年 4 月

Dravenessdraveness 维护。



  • Draveness

  • [x] 易于集成,高性能
  • [x] 提供 UIKit 和 CoreAnimation 分类
  • [x] 从文件中读取颜色自定义
  • [x] 支持不同的主题
  • [x] 用一行宏生成其他库的 picker

演示


如果您想在不导入 Objective-C 代码的情况下在 Swift 项目中实现夜间模式,这是 Swift 版本 NightNight

如何开始

DKNightVersion 支持多种方法在项目中安装库。

Podfile

要使用 CocoaPods 将 DKNightVersion 集成到您的 Xcode 项目中,请在您的 Podfile 中指定它

pod "DKNightVersion"

然后,运行以下命令:

$ pod install

导入

导入 DKNightVersion 头文件

#import <DKNightVersion/DKNightVersion.h>

使用

在您的项目中检出 DKColorTable.txt 文件,它位于 Pods/DKNightVersion/Resources/DKNightVersion.txt

NORMAL   NIGHT
#ffffff  #343434 BG
#aaaaaa  #313131 SEP

您还可以创建另一个颜色表文件,并通过 DKColorTable 指定它。

然后,使用 DKColorPickerWithKey 设置颜色选择器,它会生成一个 DKColorPicker 块

self.view.dk_backgroundColorPicker = DKColorPickerWithKey(BG);

当当前主题版本更改为 DKThemeVersionNight 时,视图背景颜色将切换到 #343434

[DKNightVersionManager nightFalling];

或通过管理器的属性 themeVersion 直接更改主题版本,它是一个字符串

DKNightVersionManager *manager = [DKNightVersionManager sharedInstance];
manager.themeVersion = DKThemeVersionNormal;

高级使用

您可以使用两种方法将夜间模式集成到您的 iOS 应用中。

DKNightVersionManager

DKNightVersion 的最新版本为NSObject 添加了一个只读的 `dk_manager` 属性,它返回 DKNightVersionManager 单例。

更改主题

您可以调用 nightFallingdawnComing 来切换当前主题版本到 DKThemeVersionNightDKThemeVersionNormal

[self.dk_manager dawnComing];
[self.dk_manager nightFalling];

通过修改 themeVersion 属性直接切换主题版本。

self.dk_manager.themeVersion = DKThemeVersionNormal;
self.dk_manager.themeVersion = DKThemeVersionNight;
// if there is a RED column in DKColorTable.txt (default) or in 
// other `file` if you customize `file` property for `DKColorTable`
self.dk_manager.themeVersion = @"RED"; 

发帖通知

每次当前主题版本发生更改时,DKNightVersionManager将发布一个DKNightVersionThemeChangingNotification。如果您想要进行一些定制,您可以观察这个通知并采取适当的行动。

DKColorPicker

DKColorPicker是DKNightVersion的核心,这个库为每个UIKit和Core Animation组件添加了dk_colorPicker。例如:

@property (nonatomic, copy, setter = dk_setBackgroundColorPicker:) DKColorPicker dk_backgroundColorPicker;
@property (nonatomic, copy, setter = dk_setTintColorPicker:) DKColorPicker dk_tintColorPicker;

DKColorPicker在DKColor.h文件中定义,它接受一个DKThemeVersion参数并返回一个UIColor

typedef UIColor *(^DKColorPicker)(DKThemeVersion *themeVersion);
  • 使用DKColorPickerWithKey(key)DKColorTable中获取DKColorPicker

    view.dk_backgroundColorPicker = DKColorPickerWithKey(BG);
  • 使用DKColorPickerWithRGB生成DKColorPicker

    view.dk_backgroundColorPicker =  DKColorPickerWithRGB(0xffffff, 0x343434);

DKColorTable

DKColorTable是DKNightVersion的新功能,它以优雅的方式提供了一个在项目中管理颜色设置的方法。使用方法如下:

有一个名为DKColorTable.txt的文件。

NORMAL   NIGHT
#ffffff  #343434 BG
#aaaaaa  #313131 SEP

该文件的第一个行指示不同的主题。强烈建议使用NORMAL列,其他列为可选。因此,如果您不希望在您的应用程序中集成不同的主题,只需在文件中将第一列留空,如下所示

NORMAL
#ffffff BG
#aaaaaa SEP

NORMALNIGHT是两个不同的主题,NORMAL是默认主题,用于正常模式。而NIGHT是可选的,用于夜间模式。

您可以在DKColorTable.txt文件中添加多个列,数量根据需要而定。

NORMAL   NIGHT    RED
#ffffff  #343434  #ff0000 BG
#aaaaaa  #313131  #ff0000 SEP

最后一列是颜色条目的密钥,DKNightVersion使用当前的主题版本(例如:NORMALNIGHTRED)和密钥(例如:BGSEP)在DKColorTable中查找相应的颜色。

DKColorTable有一个属性file,当调用+ [DKColorTable sharedColorTable时,它将加载此file中的颜色设置。默认值是DKColorTable.txt

@property (nonatomic, strong) NSString *file;

您也可以将另一个文件添加到您的项目中,在那里填写您的颜色设置。

// color.txt
NORMAL   NIGHT
#ffffff  #343434 BG

并更改file

[DKColorTable sharedColorTable].file = @"color.txt"

这将从color.txt文件重新加载颜色设置。

创建临时DKColorPicker

如果您想创建一些临时的DKColorPicker,您可以使用这些方法。

view.dk_backgroundColorPicker =  DKColorPickerWithRGB(0xffffff, 0x343434);

DKColorPickerWithRGB将返回一个DKColorPicker,当当前主题版本为DKThemeVersionNormal时,背景颜色设置为#ffffff,当它是DKThemeVersionNight时,背景颜色设置为#343434

还有一些类似的功能,如DKColorPickerWithColors

DKColorPicker DKColorPickerWithRGB(NSUInteger normal, ...);
DKColorPicker DKColorPickerWithColors(UIColor *normalColor, ...);

DKColor还提供了一组方便的API,这些API返回返回同一颜色的DKColorPicker块,这些块在不同主题中返回相同颜色。

+ (DKColorPicker)colorPickerWithUIColor:(UIColor *)color;

+ (DKColorPicker)colorPickerWithWhite:(CGFloat)white alpha:(CGFloat)alpha;
+ (DKColorPicker)colorPickerWithHue:(CGFloat)hue saturation:(CGFloat)saturation brightness:(CGFloat)brightness alpha:(CGFloat)alpha;
+ (DKColorPicker)colorPickerWithRed:(CGFloat)red green:(CGFloat)green blue:(CGFloat)blue alpha:(CGFloat)alpha;
+ (DKColorPicker)colorPickerWithCGColor:(CGColorRef)cgColor;
+ (DKColorPicker)colorPickerWithPatternImage:(UIImage *)image;
#if __has_include(<CoreImage/CoreImage.h>)
+ (DKColorPicker)colorPickerWithCIColor:(CIColor *)ciColor NS_AVAILABLE_IOS(5_0);
#endif

+ (DKColorPicker)blackColor;
+ (DKColorPicker)darkGrayColor;
+ (DKColorPicker)lightGrayColor;
+ (DKColorPicker)whiteColor;
+ (DKColorPicker)grayColor;
+ (DKColorPicker)redColor;
+ (DKColorPicker)greenColor;
+ (DKColorPicker)blueColor;
+ (DKColorPicker)cyanColor;
+ (DKColorPicker)yellowColor;
+ (DKColorPicker)magentaColor;
+ (DKColorPicker)orangeColor;
+ (DKColorPicker)purpleColor;
+ (DKColorPicker)brownColor;
+ (DKColorPicker)clearColor;

pickerify

DKNightVersion提供了一个非常强大的功能,可以通过宏pickerify自动生成dk_xxxColorPicker。

@pickerify(TableViewCell, cellTintColor)

这将自动为您生成dk_cellTintColorPicker

DKImagePicker

使用DKImagePicker,当manager.themeVersion变化时更改图像。

imageView.dk_imagePicker = DKImagePickerWithNames(@"normal", @"night");

第一张图像用于NORMAL主题,第二张图像用于NIGHT主题,因为DKColorTable.txt文件中主题的排序为NORMAL NIGHT。

如果您的文件如下所示:

NORMAL   NIGHT    RED
#ffffff  #343434  #fafafa BG
#aaaaaa  #313131  #aaaaaa SEP
#0000ff  #ffffff  #fa0000 TINT
#000000  #ffffff  #000000 TEXT
#ffffff  #444444  #ffffff BAR

按照以下顺序设置您的图像选择器

imageView.dk_imagePicker = DKImagePickerWithNames(@"normal", @"night", @"red");

图像或名称的顺序与DKColorTable.txt文件中的顺序完全相同。

DKImagePicker DKImagePickerWithImages(UIImage *normalImage, ...);
DKImagePicker DKImagePickerWithNames(NSString *normalName, ...);

贡献

如有需要帮助或发现错误,请随时提出issues或pull request。

联系方式

待办事项

  • 文档

许可证

DKNightVersion基于MIT许可证可用。更多信息请参阅LICENSE文件。

MIT许可证(MIT)

版权所有 (c) 2015 Draveness

任何人获得本软件及其相关文档文件的副本(以下简称“软件”),在此特此免费许可,不受任何限制地处理该软件,包括但不限于使用、复制、修改、合并、发布、分发、再许可和/或销售软件的副本,并允许获得软件的个人从事上述活动,前提是遵守以下条件

上述版权声明和本许可声明应包含在软件的所有副本或主要部分中。

本软件按“原样”提供,不提供任何形式的保证,无论是明确的还是暗示的,包括但不限于适销性、特定用途的适用性和非侵权性。在任何情况下,作者或版权所有者对于任何索赔、损害或其他责任,无论出于合同、侵权或其他原因,因使用或与其他软件或使用等方法结合使用而产生的,均不予承担责任。