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

标签 ios xcode colors palette

我想为我的 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/

相关文章:

iOS GLSL 球谐着色器。如何使用 iOS GPU 进行 Raycast

iphone - 编辑模式下的自定义 UITableViewCell 不会移动我的 UILabel

ios - UITextField 边框颜色

delphi - 如何更改 TCheckBox、TRadioButton、TGroupBox 上的标题字体颜色?

ios - 今日 View 小部件中的透明文本颜色

ios - 自定义字体无法在 swift 中以编程方式工作

c - 使用 XCode 通过 K&R 学习 C,但错误太多

c++ - rand() 在 OS X Mavericks/XCode 5.1 上坏了吗? (或者, "is this a massive security hole, or am I just a bad programmer??")

ios - 表达式从 'UIView?' 隐式强制转换为 Any - 如何在源代码中查找行

C# 随机颜色生成器无法正常工作