美文网首页
YYText中YYLabel和YYTextView适配暗黑模式

YYText中YYLabel和YYTextView适配暗黑模式

作者: 富春江水 | 来源:发表于2021-12-09 10:53 被阅读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

相关文章

网友评论

      本文标题:YYText中YYLabel和YYTextView适配暗黑模式

      本文链接:https://www.haomeiwen.com/subject/vucffrtx.html