https://github.com/rayjiang16/xycolor
An easy way to adapter dark mode on CALayer. iOS 快速适配夜间模式
https://github.com/rayjiang16/xycolor
color darkmode swift5
Last synced: 4 months ago
JSON representation
An easy way to adapter dark mode on CALayer. iOS 快速适配夜间模式
- Host: GitHub
- URL: https://github.com/rayjiang16/xycolor
- Owner: RayJiang16
- License: mit
- Created: 2019-07-22T01:27:50.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2020-02-07T02:58:12.000Z (over 5 years ago)
- Last Synced: 2025-06-13T20:09:08.021Z (4 months ago)
- Topics: color, darkmode, swift5
- Language: Swift
- Homepage:
- Size: 50.8 KB
- Stars: 80
- Watchers: 1
- Forks: 8
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# XYColor
[](https://github.com/RayJiang16/XYColor/actions?query=workflow%3Abuild)
[](https://cocoapods.org/pods/XYColor)
[](https://github.com/Carthage/Carthage)
[](./)
[](./LICENSE)An easy way to adapter dark mode on CALayer
[中文介绍](README_CN.md)
## Requirements
- iOS 8.0+
- Swift 5.0+
- Xcode 11.0+## Installation
**Installation with [CocoaPods](https://cocoapods.org/):**
- Swift
```
pod 'XYColor'
```
- Objective-C
```
pod 'XYColorOC'
```**Installation with [Carthage](https://github.com/Carthage/Carthage):**
```
github "RayJiang16/XYColor"
```
Run `carthage` to build the frameworks and drag the appropriate framework (`XYColor.framework` or `XYColorOC.framework`) into your Xcode project based on your need. Make sure to add only one framework and not both.## Usage
As we all known `UIViewController` and `UIView` has `traitCollection.userInterfaceStyle` property, but `CALayer` doesn't.
Therefor `CALayer` wants to adapter dark mode, it need to bind on a `UIView`.
That means `CALayer` will change color when `UIView.traitCollection.userInterfaceStyle` changed.**Swift**
```swift
import XYColor// View
private var customView: UIView = {
let view = UIView()
...
view.setLayerBorderColor(UIColor.label)
view.setLayerShadowColor(UIColor.label)
view.setLayerBackgroundColor(UIColor.systemBackground)
...
return view
}()// Layer
private var customLayer: CALayer = {
let layer = CALayer()
...
layer.setBorderColor(UIColor.label, with: customView)
layer.setShadowColor(UIColor.label, with: customView)
layer.setBackgroundColor(UIColor.systemBackground, with: customView)
...
return layer
}()// Create color
private var color: UIColor = {
return UIColor.create(light: .black, dark: .white)
}()
```**Objective-C**
```objective-c
#import "XYColorOC/XYColorOC.h"// View
- (UIView *)customView {
...
[_customView xy_setLayerBorderColor:UIColor.labelColor];
[_customView xy_setLayerShadowColor:UIColor.labelColor];
[_customView xy_setLayerBackgroundColor:UIColor.systemBackgroundColor];
...
}// Layer
- (CALayer *)customLayer {
...
[_customLayer xy_setLayerBorderColor:UIColor.labelColor with:self.customView];
[_customLayer xy_setLayerShadowColor:UIColor.labelColor with:self.customView];
[_customLayer xy_setLayerBackgroundColor:UIColor.systemBackgroundColor with:self.customView];
...
}// Create color
- (UIColor *)color {
if (!_color) {
_color = [UIColor xy_createWithLightColor:UIColor.blackColor darkColor:UIColor.whiteColor];
}
return _color;
}
```## License
**XYColor** is under MIT license. See the [LICENSE](LICENSE) file for more info.