LoginSignup
6
6

More than 5 years have passed since last update.

swift3.0でUIColorの取得法が変わったのでよく使ってるExtentionも書き換えておく。

Posted at

Swift3系になってからUIColorがクラス変数になりましたよね。
UIColor.white() → UIColor.whiteみたいな。
Extentionを昔のまま使い回すと、UIColor.baseBlue()みたいになって統一感でないので、Swift3.x系のプロジェクトに入れる分は書き換えておきました。

UIColor+AppColor.swift(Swift3.x)
import UIKit
import Foundation

extension UIColor {

    open class var baseBlue: UIColor {
        return UIColor.rgbColor(0x289BCC)
    }

    open class var baseOrange: UIColor {
        return UIColor.rgbColor(0x289BCC)
    }
    open class var darkOrange: UIColor {
        return UIColor.rgbColor(0x289BCC)
    }
    open class var lightOrange: UIColor {
        return UIColor.rgbColor(0x289BCC)
    }

    class func rgbColor(_ rgbValue: UInt) -> UIColor{
        return UIColor(
            red:   CGFloat((rgbValue & 0xFF0000) >> 16) / 255.0,
            green: CGFloat((rgbValue & 0x00FF00) >>  8) / 255.0,
            blue:  CGFloat( rgbValue & 0x0000FF)        / 255.0,
            alpha: CGFloat(1.0)
        )
    }
}

ちなみにSwift2.x系のプロジェクトで使ってた方。
まだまだ2.xで開発するなら、コードの統一感的にこっち入れたほうがいいです。

UIColor+AppColor.swift(Swift2.x)
import UIKit
import Foundation

extension UIColor {

    class func baseBlue()->UIColor{
        return UIColor.rgbColor(0x289BCC)
    }

    class func baseOrange()->UIColor{
        return UIColor.rgbColor(0xFFA700)
    }

    class func darkOrange()->UIColor{
        return UIColor.rgbColor(0xD25B00)
    }

    class func lightOrange()->UIColor{
        return UIColor.rgbColor(0xFFBD3F)
    }

    class func rgbColor(rgbValue: UInt) -> UIColor{
        return UIColor(
            red:   CGFloat((rgbValue & 0xFF0000) >> 16) / 255.0,
            green: CGFloat((rgbValue & 0x00FF00) >>  8) / 255.0,
            blue:  CGFloat( rgbValue & 0x0000FF)        / 255.0,
            alpha: CGFloat(1.0)
        )
    }
}

6
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
6