gpt4 book ai didi

ios - 如何在 Xcode 中定义配色方案

转载 作者:可可西里 更新时间:2023-11-01 03:17:40 26 4
gpt4 key购买 nike

我想为我的 iOS 项目定义一个配色方案,以一种可以轻松替换某些颜色的方式。假设我有一个“主要”颜色和一个“次要”颜色,在我的应用程序的许多元素中使用,将来我可能想将“主要”颜色设置为它当前拥有的任何其他值。

到目前为止,我一直在使用自定义 UIColor 类别在代码中定义和使用我的颜色,并创建一个具有相同颜色的调色板以便在 IB 和 Storyboard中使用它。

通过这种方式,在代码中替换颜色非常简单,但在 IB 和 Storyboard 中这样做非常痛苦...我没有找到查找/替换颜色的简单方法。 p>

关于如何做到这一点的任何想法?我愿意接受任何建议。提前谢谢你

编辑 1: 问题可能不够清楚。我想创建一个方案,例如我可以在代码和 IB 中使用它,但只定义一次颜色,并且能够以代码和 IB 中引用的颜色相应更改的方式切换颜色。

最佳答案

我可以在 Swift 中做到这一点的简单方法:

  • 首先,我们必须根据需要创建尽可能多的目标方案,点击实际方案并选择新方案:

    enter image description here

  • 在 Assets Catalog Colors 中创 build 置 Colors.xcassets 颜色如下:

    Color at Colors.xcassets

  • 然后在 Colors.xcassets 选择一个目标成员:

    enter image description here

  • 之后创建另一个Colors.xcassets:

    enter image description here

  • 然后在这个新的 Colors.xcassets 中再次创建另一种相同的颜色:

    enter image description here

  • 然后在这个新的Colors.xcassets中选择另一个Target membership:

    enter image description here

  • 然后使用引用的相同名称(我们使用 PrimaryColor)通过代码分配颜色,如下例所示:

    struct Resources {

    struct Colors {
    //struct is extended in Colours
    }
    }

    extension Colors {

    struct Primary {
    static let Default = UIColor(named: "PrimaryColor")!
    }
    }
  • 然后是实现:

    newLabel.backgroundColor = Resources.Colors.Primary.Default

Finally depending on the scheme chosen, some colors or others will be drawn.

我发现的另一种方法是下一个链接:

Protocol-Oriented Themes for iOS Apps

关于ios - 如何在 Xcode 中定义配色方案,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23413403/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com