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重用模式应该遵循.
解决方法
您可以使用任何单例类或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")