1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556 |
- //
- // YMHexColor.swift
- // MvpGoods
- //
- // Created by 小花 on 2019/3/18.
- // Copyright © 2019年 MVP. All rights reserved.
- //
- import UIKit
- extension UIColor{
- class func hexadecimalColor(hexadecimal:String)->UIColor{
- var cstr = hexadecimal.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines).uppercased() as NSString;
- if(cstr.length < 6){
- return UIColor.clear;
- }
- if(cstr.hasPrefix("0X")){
- cstr = cstr.substring(from: 2) as NSString
- }
- if(cstr.hasPrefix("#")){
- cstr = cstr.substring(from: 1) as NSString
- }
- if(cstr.length != 6){
- return UIColor.clear;
- }
- var range = NSRange.init()
- range.location = 0
- range.length = 2
- //r
- let rStr = cstr.substring(with: range);
- //g
- range.location = 2;
- let gStr = cstr.substring(with: range)
- //b
- range.location = 4;
- let bStr = cstr.substring(with: range)
- var r :UInt32 = 0x0;
- var g :UInt32 = 0x0;
- var b :UInt32 = 0x0;
- Scanner.init(string: rStr).scanHexInt32(&r);
- Scanner.init(string: gStr).scanHexInt32(&g);
- Scanner.init(string: bStr).scanHexInt32(&b);
- return UIColor.init(red: CGFloat(r)/255.0, green: CGFloat(g)/255.0, blue: CGFloat(b)/255.0, alpha: 1);
- }
-
- class func gloadGrayColor()->UIColor {
- return self.hexadecimalColor(hexadecimal: "#f5f4f4");
- }
-
- class func gloadRedColor()->UIColor {
- return self.hexadecimalColor(hexadecimal: "#F0845F");
- }
- }
|