xyc

XYColor

An easy way to adapter dark mode on CALayer. iOS 快速适配夜间模式

Showing:

Popularity

Downloads/wk

0

GitHub Stars

69

Maintenance

Last Commit

2yrs ago

Contributors

0

Package

Dependencies

0

License

MIT

Categories

Readme

XYColor

GitHub Actions CocoaPods Compatible Carthage Compatible Platform License

An easy way to adapter dark mode on CALayer

中文介绍

Requirements

  • iOS 8.0+
  • Swift 5.0+
  • Xcode 11.0+

Installation

Installation with CocoaPods:

  • Swift
pod 'XYColor'
  • Objective-C
pod 'XYColorOC'

Installation with 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

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

#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 file for more info.

Rate & Review

Great Documentation0
Easy to Use0
Performant0
Highly Customizable0
Bleeding Edge0
Responsive Maintainers0
Poor Documentation0
Hard to Use0
Slow0
Buggy0
Abandoned0
Unwelcoming Community0
100