A library for backporting Dark Mode in iOS
Перейти к файлу
Levin Li 402f09f754 Define SWIFT_PACKAGE for private 2020-07-24 20:25:10 +08:00
.github/workflows Integrate SwiftLint and fix CI (#68) 2020-05-21 16:54:23 +08:00
Assets Add header image for README (#54) 2020-02-28 12:13:26 +08:00
FluentDarkModeKit.xcodeproj Private header #1 2020-07-24 20:15:31 +08:00
Sources Define SWIFT_PACKAGE for private 2020-07-24 20:25:10 +08:00
Tests Add theme change handler 2020-07-24 20:14:08 +08:00
.gitattributes Add header image for README (#54) 2020-02-28 12:13:26 +08:00
.gitignore Re-structure example (#25) 2019-11-05 16:24:40 +08:00
.swiftlint.yml Integrate SwiftLint and fix CI (#68) 2020-05-21 16:54:23 +08:00
CODE_OF_CONDUCT.md Initial CODE_OF_CONDUCT.md commit 2019-10-24 01:06:11 -07:00
FluentDarkModeKit.podspec Bump version to 0.5.2 2020-06-08 08:46:31 +08:00
LICENSE Initial LICENSE commit 2019-10-24 01:06:12 -07:00
Package.swift SPM support 2020-07-24 20:15:31 +08:00
README.md Fix importing via spm (#69) 2020-05-26 10:17:37 +08:00
SECURITY.md Initial SECURITY.md commit 2019-10-24 01:06:13 -07:00

README.md

Fluent Dark Mode Kit

Carthage compatible

FluentDarkModeKit was designed and developed before Apples official dark mode release. It provides a mechanism to support dark mode for apps on iOS 11+ (including iOS 13).

Features

  • Dark Mode support for iOS 11+
  • Dynamic theme change within the app without restart
  • Simple API design with minimal changes to your existing code

Installation

Requirements

  • iOS 11.0+
  • Xcode 11.0+
  • Swift 5.0+
  • Git LFS

You can use Homebrew to install Git LFS:

brew install git-lfs

Carthage

To integrate FluentDarkModeKit into your Xcode project using Carthage, specify it in your Cartfile:

github "microsoft/FluentDarkModeKit"

Swift Package Manager

Click "Files -> Swift Package Manager -> Add Package Dependency..." in Xcode's menu and search "https://github.com/microsoft/FluentDarkModeKit"

Since Xcode uses its own tools to handle swifft packages, git-lfs might not be picked up if it is installed via Homebrew. Run the following command to create a symbolic link if Xcode fails to fetch the package:

ln -s /usr/local/bin/git-lfs $(xcode-select -p)/usr/bin/git-lfs

CocoaPods

To integrate FluentDarkModeKit into your Xcode project using CocoaPods, specify it in your Podfile:

pod "FluentDarkModeKit"

Get Started

How to Use FluentDarkModeKit

To use FluentDarkModeKit, provide a pair of colors or images instead of a single value. Simply replace existing colors/images with a pair of light and dark colors/images.

Colors

Swift

extension UIColor {
    init(_: DMNamespace, light: UIColor, dark: UIColor)
}

let color = UIColor(.dm, light: .white, dark: .black)

Objective-C

@interface UIColor (FluentDarkModeKit)
- (UIColor *)dm_colorWithLightColor:(UIColor *)lightColor darkColor:(UIColor *)darkColor;
@end

UIColor *color = [UIColor dm_colorWithLightColor:UIColor.whiteColor darkColor:UIColor.blackColor];

Images

Swift

extension UIImage {
    init(_: DMNamespace, light: UIImage, dark: UIImage)
}

let lightImage = UIImage(named: "Light")!
let darkImage = UIImage(named: "Dark")!
let image = UIImage(.dm, light: lightImage, dark: darkImage)

Objective-C

@interface UIImage (FluentDarkModeKit)
- (UIImage *)dm_imageWithLightImage:(UIImage *)lightImage darkImage:(UIImage *)darkImage;
@end

Others

For more complex scenarios, FluentDarkModeKit is similar to what Apple does in iOS 13 (some slight differences). It maintains a global DMTraitCollection which can be accessed through DMTraitCollection.current during custom layout.

For more information on cases you should handle during layout, please refer to Apple's documentation.

FluentDarkModeKit will notify views or view controllers in the current window when the theme changes by calling the following delegate method.

Swift

protocol DMTraitEnvironment: NSObjectProtocol {
    func dmTraitCollectionDidChange(_ previousTraitCollection: DMTraitCollection?)
}

Objective-C

@protocol DMTraitEnvironment <NSObject>

- (void)dmTraitCollectionDidChange:(nullable DMTraitCollection *)previousTraitCollection;

@end

Contributing

This project welcomes contributions and suggestions. Most contributions require you to agree to a Contributor License Agreement (CLA) declaring that you have the right to, and actually do, grant us the rights to use your contribution. For details, visit https://cla.opensource.microsoft.com.

When you submit a pull request, a CLA bot will automatically determine whether you need to provide a CLA and decorate the PR appropriately (e.g., status check, comment). Simply follow the instructions provided by the bot. You will only need to do this once across all repos using our CLA.

This project has adopted the Microsoft Open Source Code of Conduct. For more information see the Code of Conduct FAQ or contact opencode@microsoft.com with any additional questions or comments.

License

Copyright (c) Microsoft Corporation. All rights reserved.

Licensed under the MIT license.