YYText中YYLabel和YYTextView适配暗黑模式

2021-12-09  本文已影响0人  富春江水

YYTextView 和YYLabel 适配暗黑模式完美解决的前提是 UIColor 必须正确适配

NSMutableAttributedString中必须要传NSForegroundColorAttributeName,适配好颜色


YYLabel.m 添加如下代码

#pragma mark - DarkMode Adapater

#ifdef __IPHONE_13_0

- (void)traitCollectionDidChange:(UITraitCollection *)previousTraitCollection{

    [super traitCollectionDidChange:previousTraitCollection];

    if (@available(iOS 13.0, *)) {

        if([UITraitCollection.currentTraitCollection hasDifferentColorAppearanceComparedToTraitCollection:previousTraitCollection]){

            [self.layer setNeedsDisplay];

        }

    } else {

        // Fallback on earlier versions

    }

}

#endif

YYTextView.m 添加如下代码

#pragma mark - Dark mode Adapter

#ifdef __IPHONE_13_0

- (void)traitCollectionDidChange:(UITraitCollection *)previousTraitCollection{

    [super traitCollectionDidChange:previousTraitCollection];

    if (@available(iOS 13.0, *)) {

        if([UITraitCollection.currentTraitCollection hasDifferentColorAppearanceComparedToTraitCollection:previousTraitCollection]){

            [self _commitUpdate];

        }

    } else {

        // Fallback on earlier versions

    }

}

#endif

上一篇下一篇

猜你喜欢

热点阅读