将自定义UIColor引入Swift项目的最佳方法是什么? [英] What is the best way to introduce a custom UIColor to a Swift project?

查看:107
本文介绍了将自定义UIColor引入Swift项目的最佳方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想过两种类型的实现;在性能,可读性和可维护性方面,您认为其中哪一个更好?

I've thought of two types of implementation; which one of them is better in your opinion in terms of performance, readability and maintainability?


  1. 创建UIColor的扩展名,如下所示

  1. Create an extension of UIColor like this

extension UIColor {
    class func myColor() -> UIColor {
        return UIColor(red: 128/255, green: 102/255, blue: 0, alpha: 1)
    }
}


  • 创建结构:

  • Create a structure:

    struct Colors {
        static let myColor = UIColor(red: 255/255, green: 102/255, blue: 0, alpha: 1)
    }
    



  • 推荐答案

    答案:扩展,在我的专业意见。

    Answer: Extension, in my professional opinion.

    考虑一下;在哲学上,你是扩展 UIColor 提供的颜色范围。如果您的颜色名称是不同的,并且新功能遵循Apple的方法命名协议(即<颜色名称>颜色),则扩展 UIColor 看起来更整洁。一个或两个新颜色(在我看来)不保证整个专用的结构

    Think about it; you are, in philosophy, 'extending' the range of colors offered by UIColor. Provided that your color name is distinct and the new function follows Apple's method naming protocol (i.e. <color name>Color), extending UIColor seems neater. One or two new colors (in my opinion) don't warrant an entire dedicated struct.

    奖励回答:
    struct (或 enum !)是不是很合适?

    Bonus answer: Where would a struct (or enum!) be a good fit?


    • 如果您的应用强制替换标准颜色(即自定义
      ) '主要'颜色)

    • 如果您的应用专门设计为主题/自定义,那么可能有一个枚举,作为可用选项的具体列表。

    • 如果你想不出颜色的标准名称( sharkBlueColor ,有人吗?)。

    • 如果您的应用专门用于绘图/绘画(在这种情况下,调色板构造可能是个好主意)。

    • If your app forces replacements for the standard colors (i.e. custom 'primary' colors)
    • If your app is specifically designed to be themed / customized, it might be good have an enum, to serve as a concrete list for available options.
    • If you can't think of standard names for the colors (sharkBlueColor, anyone?).
    • If your app is specifically for drawing/painting (in which case a 'palette' construct might be good idea).

    ......列表还在继续。当你成熟为Swift开发者时,你必须学会​​辨别和决定自己!

    ... the list goes on. You must learn to discern and decide for yourself as you mature as a Swift developer!

    这篇关于将自定义UIColor引入Swift项目的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆