如何重用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")

相关文章

UITabBarController 是 iOS 中用于管理和显示选项卡界面的一...
UITableView的重用机制避免了频繁创建和销毁单元格的开销,使...
Objective-C中,类的实例变量(instance variables)和属性(...
从内存管理的角度来看,block可以作为方法的传入参数是因为b...
WKWebView 是 iOS 开发中用于显示网页内容的组件,它是在 iO...
OC中常用的多线程编程技术: 1. NSThread NSThread是Objecti...