优惠券swift版马甲包

YMHexColor.swift 1.6KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. //
  2. // YMHexColor.swift
  3. // MvpGoods
  4. //
  5. // Created by 小花 on 2019/3/18.
  6. // Copyright © 2019年 MVP. All rights reserved.
  7. //
  8. import UIKit
  9. extension UIColor{
  10. class func hexadecimalColor(hexadecimal:String)->UIColor{
  11. var cstr = hexadecimal.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines).uppercased() as NSString;
  12. if(cstr.length < 6){
  13. return UIColor.clear;
  14. }
  15. if(cstr.hasPrefix("0X")){
  16. cstr = cstr.substring(from: 2) as NSString
  17. }
  18. if(cstr.hasPrefix("#")){
  19. cstr = cstr.substring(from: 1) as NSString
  20. }
  21. if(cstr.length != 6){
  22. return UIColor.clear;
  23. }
  24. var range = NSRange.init()
  25. range.location = 0
  26. range.length = 2
  27. //r
  28. let rStr = cstr.substring(with: range);
  29. //g
  30. range.location = 2;
  31. let gStr = cstr.substring(with: range)
  32. //b
  33. range.location = 4;
  34. let bStr = cstr.substring(with: range)
  35. var r :UInt32 = 0x0;
  36. var g :UInt32 = 0x0;
  37. var b :UInt32 = 0x0;
  38. Scanner.init(string: rStr).scanHexInt32(&r);
  39. Scanner.init(string: gStr).scanHexInt32(&g);
  40. Scanner.init(string: bStr).scanHexInt32(&b);
  41. return UIColor.init(red: CGFloat(r)/255.0, green: CGFloat(g)/255.0, blue: CGFloat(b)/255.0, alpha: 1);
  42. }
  43. class func gloadGrayColor()->UIColor {
  44. return self.hexadecimalColor(hexadecimal: "#f5f4f4");
  45. }
  46. class func gloadRedColor()->UIColor {
  47. return self.hexadecimalColor(hexadecimal: "#F0845F");
  48. }
  49. }