如何在Xcode中定义颜色方案

13
我希望为我的iOS项目定义一个颜色方案,使得替换某些颜色更加容易。假设我有一个“主”颜色和一个“次要”颜色,它们在我的应用程序的许多元素中使用,并且将来可能想将“主”颜色设置为当前值之外的任何其他值。
到目前为止,我一直在使用自定义的UIColor类别来定义和使用我的颜色代码,并创建具有相同颜色的调色板以在IB和Storyboards中使用。
这样,在代码中替换颜色就变得简单得多,但是在IB和Storyboard中进行替换非常痛苦...我找不到一种简单的方法来查找/替换颜色。
有什么办法可以实现这样的管理吗?我对任何建议都持开放态度。提前谢谢!
编辑1:也许问题没有表述清楚。我想创建一个方案,可以在代码和IB中同时使用,但只定义一次颜色,并能够以一种方式切换颜色,以便在代码和IB中引用的颜色相应更改。

听起来你想看一下 UIAppearance。它允许你改变整个应用程序的外观(例如颜色和色调)。唯一的缺点是主题不能在 IB 中可见。 - David Berry
@David 我知道UIAppearance的功能,我正在将其与我的UIColor类别一起使用。正如你所提到的,我不知道的是如何在IB和Storyboards中做类似的事情。 - sonxurxo
我认为没有办法。你可能可以解析.storyboard文件(它只是复杂的xml)和/或使用脚本进行修补。 - David Berry
我查看了那个文件,至少想找到RGB、HEX或类似的代码,但我发现在.storyboard文件中,颜色是用红、绿、蓝和透明度的浮点值描述的,或者是用白色和透明度,这取决于您在IB中如何定义它们。这真是一场噩梦! - sonxurxo
看起来对我来说这是XSLT的工作 :) - David Berry
有没有什么可靠的方法来解决这个问题? - Bista
2个回答

2

在Swift中,我可以轻松地这样做:

  • First we have to create as many targets as we want schemes, doing click at actual scheme and select New scheme:

    enter image description here

  • Creating at Assets Catalog Colors Set Colors.xcassets a color like:

    Color at Colors.xcassets

  • Then select one Target membership at Colors.xcassets:

    enter image description here

  • After that create another Colors.xcassets:

    enter image description here

  • Then create again another color with same same at this new Colors.xcassets:

    enter image description here

  • Then select another one Target membership at this new Colors.xcassets:

    enter image description here

  • Then assign color by code using same name referenced (we use PrimaryColor), as in the following example:

    struct Resources {
    
        struct Colors {
            //struct is extended in Colours
        }
    }
    
    extension Colors {
    
        struct Primary {
           static let Default = UIColor(named: "PrimaryColor")!
        }
    }
    
  • And then the implementation:

    newLabel.backgroundColor = Resources.Colors.Primary.Default
    

最终根据所选方案,将绘制一些颜色或其他颜色。

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

iOS应用的基于协议的主题


2
你可以选择循环查找在父视图中添加的子视图,并定义一个你想要改变颜色的元素集合。
你的代码可能像下面这样:
-(void)defineColors:(UIView *)parent{

    for(UIView *view in parent.subviews){

      if(view.tag == TAG1){
        [self setColor:COLOR1 forControl:view];
      }else if(view.tag == TAG2){
        [self setColor:COLOR2 forControl:view];
      }

    }

}

-(void)setColor:(UIColor *)color forControl:(UIView *)control{

      if([control isKindOfClass:[UILabel class]]){
                [(UILabel *)view setBackgroundColor:color];
      }else if([control isKindOfClass:[UITextField class]]){
                [(UITextField *)view setTextColor:color];
      }

      //So on..


}

希望对您有所帮助。

祝福。

代码已更新

您可以选择为要分配特定颜色的控件定义一组标记,然后相应地设置颜色。

更新2

根据您编辑的问题,您可以拥有一个独立的 xib,其中可以添加不同的小型 UIView,并且可以从 xib 中定义颜色。 为不同的视图创建出口。 然后在 AppDelegate 中加载nib以获取您想要在应用中使用的不同颜色,并将它们保存到 Singleton Class 中,以便您可以随时获取它们而无需多次初始化。


谢谢您的快速回复,但是如果您仔细阅读我的问题,我知道如何为代码中引用的颜色设置。此外,我没有提到我想要根据视图类型设置颜色。相反,我想为特定的视图设置特定的颜色。 - sonxurxo
@sonxurxo 我知道你知道如何做事,我提供的解决方案有多个好处,1. 你不需要创建Outlets 2. 你可以创建一组标签,为这些标签设置颜色.. 我会更新我的代码.. - iphonic
也许基于视图标签的解决方案会很有用,但我想创建一个颜色方案,这样我既可以在代码中使用,也可以在IB中使用。 - sonxurxo
你说得完全正确,是我的错误。我已经检查了我的问题并进行了编辑以使其更清晰,它有点令人困惑。我认为现在更清晰了。 - sonxurxo
1
我已经点赞你的问题,因为这是一个原创的解决方法,但它仍然没有解决从IB和Storyboards引用颜色的问题。 - sonxurxo
显示剩余2条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接