如何重用iOS / xcode中的颜色和样式?

Android,WPF以及我一直在使用的大多数平台都有办法在单个文件中重用和“集中”ui资源,如颜色和样式.

在android中它可以这样做:

在colors.xml文件中:

<color name="secondary_text_color">#ffcc67</color>

在任何视图中:

<TextView text="some text" textColor="@colors/secondary_text_color" />

iOS中有类似的东西吗?

我不是试图在iOS中复制android,但我正在努力理解什么是(如果有的话)ui重用模式应该遵循.

我遇到的唯一一件事就是在代码中定义Theme,并在代码中重用它,这是正确的方法吗?

解决方法

您可以使用任何单例类或UIColor扩展来完成此操作.以下是UIColor扩展示例.
import Foundation
import UIKit

extension UIColor
{
    class func someColor1() -> UIColor
    {
        return UIColor(red: 123.0/255.0,green: 162.0/255.0,blue: 157.0/255.0,alpha:1.0)
    }

    class func someColor2() -> UIColor
    {
        return UIColor(red: 154.0/255.0,green: 143.0/255.0,blue: 169.0/255.0,alpha:1.0)
    }
}

稍后您可以访问颜色

textField.textColor = UIColor.someColor2()

编辑:
您也可以使用NSAttributedString对样式执行相同的操作

class StyleHelper : NSObject{

    class func getSecondaryTextWithString(textString:String) -> NSAttributedString
    {
        let secondaryTextStyleAttributes: [String : AnyObject] = [
            NSForegroundColorAttributeName: UIColor.greenColor(),NSUnderlineStyleAttributeName: NSUnderlineStyle.StyleDouble.rawValue,NSFontAttributeName: UIFont.systemFontOfSize(14.0)]
        let secondaryTextStyleString = NSAttributedString(string: textString,attributes:secondaryTextStyleAttributes)
        return secondaryTextStyleString
    }
}

当你需要这种风格时,你会打电话给

someLabel.attributedText = StyleHelper.getSecondaryTextWithString("SomeText")

相关文章

当我们远离最新的 iOS 16 更新版本时,我们听到了困扰 Apple...
欧版/美版 特别说一下,美版选错了 可能会永久丧失4G,不过只...
一般在接外包的时候, 通常第三方需要安装你的app进行测...
前言为了让更多的人永远记住12月13日,各大厂都在这一天将应...