Libra Studio Log

開発に関することやゲーム、ガジェットなどについてつらつらと書き記しています

Swiftで16進法にてUIColorを取得

f:id:daihase:20190809100049p:plain

こんばんは、daihaseです。

久々のブログ更新です...。 今日はiOS開発をやっていてよくあるカラー周りの管理について。

管理といってもそんな大それたあれではなく、単にUIColorを拡張(Extension)し、各クラスから簡単に16進数を指定するだけで好きな色を取得出来るようにするってだけです。

まずUIColorの拡張コードから。

import Foundation
import UIKit

public extension UIColor {
    public convenience init(hexString: String) {
        self.init(hexString: hexString, alpha: 1.0)
    }
    
    public convenience init(hexString: String, alpha: Float) {
        var hex = hexString
        if hex.hasPrefix("#") {
            hex = hex.substring(from: hex.characters.index(hex.startIndex, offsetBy: 1))
        }
        if (hex.range(
            of: "(^[0-9A-Fa-f]{6}$)|(^[0-9A-Fa-f]{3}$)",
            options: .regularExpression
            ) != nil) {
            
            if hex.characters.count == 3 {
                let redHex   = hex.substring(to: hex.characters.index(hex.startIndex, offsetBy: 1))
                let greenHex = hex.substring(
                    with: (hex.characters.index(hex.startIndex, offsetBy: 1) ..< hex.characters.index(hex.startIndex, offsetBy: 2))
                )
                let blueHex  = hex.substring(from: hex.characters.index(hex.startIndex, offsetBy: 2))
                hex = redHex + redHex + greenHex + greenHex + blueHex + blueHex
            }
            
            let redHex = hex.substring(to: hex.characters.index(hex.startIndex, offsetBy: 2))
            let greenHex = hex.substring(
                with: (hex.characters.index(hex.startIndex, offsetBy: 2) ..< hex.characters.index(hex.startIndex, offsetBy: 4))
            )
            
            let blueHex = hex.substring(
                with: (hex.characters.index(hex.startIndex, offsetBy: 4) ..< hex.characters.index(hex.startIndex, offsetBy: 6))
            )
            
            var redInt:   CUnsignedInt = 0
            var greenInt: CUnsignedInt = 0
            var blueInt:  CUnsignedInt = 0
            
            Scanner(string: redHex).scanHexInt32(&redInt)
            Scanner(string: greenHex).scanHexInt32(&greenInt)
            Scanner(string: blueHex).scanHexInt32(&blueInt)
            
            self.init(
                red: CGFloat(redInt) / 255.0,
                green: CGFloat(greenInt) / 255.0,
                blue: CGFloat(blueInt) / 255.0,
                alpha: CGFloat(alpha)
            )
        }
        else {
            self.init()
        }
    }
}

 

これだけ。 あとは適当にColorManagerクラスとかを作って呼び出すだけです。

import UIKit

class ColorManager: NSObject {
    class func applicationBaseColor() -> UIColor {
        return UIColor(hexString: "#b2f453", alpha: 1.0)
    }
}</pre>

&nbsp;

例えば上の<span style="color: #ff0000;"><strong>applicationBaseColor</strong><span style="color: #000000;">だったら、</span></span>

<pre class="lang:swift decode:true" title="ViewController.swift">override func viewDidLoad() {
              super.viewDidLoad()
        self.view.backgroundColor = ColorManager.applicationBaseColor()
.
.
.

 

こんな感じに指定してやれば、viewの背景色をその16進数で指定した色に変えることが出来ます。

本日はここまで。 それでは良い開発ライフを〜