swift 的UIColor + Styles.swift

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了swift 的UIColor + Styles.swift相关的知识,希望对你有一定的参考价值。

//
//  UIColor+Styles.swift
//
//  Created by Joffrey Bocquet on 04/10/2017.
//  Copyright © 2017 APP APP APP. All rights reserved.
//

import Foundation
import UIKit

//MARK: -
//MARK: Extension for our themes colors organized by kind of use (components like, text, bg, Plain..)
// Using structure as namespace for theming colors

extension UIColor {
    
    
    struct Plain {
        //the main red color of l'Express
        @nonobjc static var Main: UIColor { return ~0xec1d24 }
        @nonobjc static var Error: UIColor { return ~0xdd2d40 }
        @nonobjc static var Focus: UIColor { return ~0x0098CE }
    }
    
    struct Text {
        @nonobjc static var TitleDark : UIColor { return ~0x272727 }
        @nonobjc static var SubtitleDark : UIColor { return ~0x666666 }
        @nonobjc static var mainDark : UIColor { return ~0x2b2b2b }
        @nonobjc static var WarmGray : UIColor { return ~0x888888 }
        @nonobjc static var Error : UIColor { return Plain.Error }
    }
    
    struct Background {
        @nonobjc static var Light : UIColor { return .white }
        @nonobjc static var Dark : UIColor { return .black }

    }
    
    struct Border {
        @nonobjc static var Main : UIColor { return ~0xD0D0D0 }
        // Used as the color for the text field view borders
        @nonobjc static var FormField : UIColor { return Border.Main }
        @nonobjc static var FormFieldFocus : UIColor { return Plain.Focus }
    }
}

//MARK: -
//MARK: Toolbox extension to transform hexa color numbers to UIColor or CGColor
// with a operator overload : `~` for UIColor and `~~` for CGColor

extension UIColor {
    
    
    convenience init(red: Int, green: Int, blue: Int) {
        self.init(red: CGFloat(red)/255.0, green: CGFloat(green)/255.0, blue: CGFloat(blue)/255.0, alpha: 1.0)
    }
    
    convenience init(hex: Int) {
        self.init(red: (hex >> 16) & 0xFF, green: (hex >> 8) & 0xFF, blue: (hex) & 0xFF)
    }
    
    static func interpolate(_ color1: UIColor, with color2: UIColor, at percent: CGFloat) -> UIColor {
        // Assume these are RGBA. We won't use anything else here.
        
        var r1: CGFloat = 0,
        r2: CGFloat = 0,
        g1: CGFloat = 0,
        g2: CGFloat = 0,
        b1: CGFloat = 0,
        b2: CGFloat = 0,
        a1: CGFloat = 0,
        a2: CGFloat = 0
        
        color1.getRed(&r1, green: &g1, blue: &b1, alpha: &a1)
        color2.getRed(&r2, green: &g2, blue: &b2, alpha: &a2)
        return UIColor(red: r1 + percent * (r2 - r1), green: g1 + percent * (g2 - g1), blue: b1 + percent * (b2 - b1), alpha: a1 + percent * (a2 - a1))
    }
    
    static func random() -> UIColor {
        return UIColor(red: Int(arc4random()) % 255, green: Int(arc4random()) % 255, blue: Int(arc4random()) % 255)
    }
}

prefix operator ~
prefix func ~ (right: Int) -> UIColor {
    return UIColor(hex: right)
}

prefix operator ~~
prefix func ~~ (right: Int) -> CGColor {
    return UIColor(hex: right).cgColor
}
// Here is an example of using the UIColor+Styles.swift extension

class MyViewController {
 
  func viewDidLoad() {
    super.viewDidLoad()
    self.view.backgroundColor = UIColor.Background.Light
    self.title.textColor = UIColor.Text.TitleDark
  }
}

以上是关于swift 的UIColor + Styles.swift的主要内容,如果未能解决你的问题,请参考以下文章

swift 的UIColor + extensions.swift

如何从 Swift 中的 UIColor 获取 RGB 代码(INT)[重复]

swift—UIColor十六进制

swift—UIColor十六进制

swift—UIColor十六进制

UIView 的神秘 UIColor 行为以及 Swift 中的一般行为