在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 Foundationimport UIKitextension 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 / xcode中的颜色和样式?全部内容,希望文章能够帮你解决如何重用iOS / xcode中的颜色和样式?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)