UIColor+Extension.swift 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. //
  2. // UIColor+Extension.swift
  3. // AiKeyboard
  4. //
  5. // Created by Destiny on 2025/4/23.
  6. //
  7. import UIKit
  8. // MARK: - 二、使用方法设置颜色
  9. public extension UIColor {
  10. // MARK: 2.1、根据RGBA的颜色(方法)
  11. /// 根据RGBA的颜色(方法)
  12. /// - Parameters:
  13. /// - r: red 颜色值
  14. /// - g: green颜色值
  15. /// - b: blue颜色值
  16. /// - alpha: 透明度
  17. /// - Returns: 返回 UIColor
  18. static func color(r: CGFloat, g: CGFloat, b: CGFloat, alpha: CGFloat = 1.0) -> UIColor {
  19. return UIColor(red: r / 255.0, green: g / 255.0, blue: b / 255.0, alpha: alpha)
  20. }
  21. // MARK: 2.2、十六进制字符串设置颜色(方法)
  22. static func hexStringColor(hexString: String, alpha: CGFloat = 1.0) -> UIColor {
  23. let newColor = hexStringToColorRGB(hexString: hexString)
  24. guard let r = newColor.r, let g = newColor.g, let b = newColor.b else {
  25. assert(false, "颜色值有误")
  26. return .white
  27. }
  28. return color(r: r, g: g, b: b, alpha: alpha)
  29. }
  30. // MARK: 2.3、十六进制 Int 颜色的使用(方法)
  31. /// 十六进制颜色的使用
  32. /// - Parameters:
  33. /// - color: 16进制 Int 颜色 0x999999
  34. /// - alpha: 透明度
  35. /// - Returns: 返回一个 UIColor
  36. static func hexIntColor(hexInt: Int, alpha: CGFloat = 1) -> UIColor {
  37. let redComponet: Float = Float(hexInt >> 16)
  38. let greenComponent: Float = Float((hexInt & 0xFF00) >> 8)
  39. let blueComponent: Float = Float(hexInt & 0xFF)
  40. return UIColor(red: CGFloat(redComponet / 255.0), green: CGFloat(greenComponent / 255.0), blue: CGFloat(blueComponent / 255.0), alpha: alpha)
  41. }
  42. }
  43. private extension UIColor {
  44. // MARK: 3.1、根据 十六进制字符串 颜色获取 RGB,如:#3CB371 或者 ##3CB371 -> 60,179,113
  45. /// 根据 十六进制字符串 颜色获取 RGB
  46. /// - Parameter hexString: 十六进制颜色的字符串,如:#3CB371 或者 ##3CB371 -> 60,179,113
  47. /// - Returns: 返回 RGB
  48. static func hexStringToColorRGB(hexString: String) -> (r: CGFloat?, g: CGFloat?, b: CGFloat?) {
  49. // 1、判断字符串的长度是否符合
  50. guard hexString.count >= 6 else {
  51. return (nil, nil, nil)
  52. }
  53. // 2、将字符串转成大写
  54. var tempHex = hexString.uppercased()
  55. // 检查字符串是否拥有特定前缀
  56. // hasPrefix(prefix: String)
  57. // 检查字符串是否拥有特定后缀。
  58. // hasSuffix(suffix: String)
  59. // 3、判断开头: 0x/#/##
  60. if tempHex.hasPrefix("0x") || tempHex.hasPrefix("0X") || tempHex.hasPrefix("##") {
  61. tempHex = String(tempHex[tempHex.index(tempHex.startIndex, offsetBy: 2)..<tempHex.endIndex])
  62. }
  63. if tempHex.hasPrefix("#") {
  64. tempHex = String(tempHex[tempHex.index(tempHex.startIndex, offsetBy: 1)..<tempHex.endIndex])
  65. }
  66. // 4、分别取出 RGB
  67. // FF --> 255
  68. var range = NSRange(location: 0, length: 2)
  69. let rHex = (tempHex as NSString).substring(with: range)
  70. range.location = 2
  71. let gHex = (tempHex as NSString).substring(with: range)
  72. range.location = 4
  73. let bHex = (tempHex as NSString).substring(with: range)
  74. // 5、将十六进制转成 255 的数字
  75. var r: UInt64 = 0, g: UInt64 = 0, b: UInt64 = 0
  76. Scanner(string: rHex).scanHexInt64(&r)
  77. Scanner(string: gHex).scanHexInt64(&g)
  78. Scanner(string: bHex).scanHexInt64(&b)
  79. return (r: CGFloat(r), g: CGFloat(g), b: CGFloat(b))
  80. }
  81. // MARK: 3.2、根据 十六进制值 颜色获取 RGB, 如:0x3CB371 -> 60,179,113
  82. /// 根据 十六进制值 颜色获取 RGB, 如:0x3CB371 -> 60,179,113
  83. /// - Parameter hexInt: 十六进制值,如:0x3CB37
  84. /// - Returns: 返回 RGB
  85. static func hexIntToColorRGB(hexInt: Int) -> (r: CGFloat, g: CGFloat, b: CGFloat) {
  86. let red: CGFloat = CGFloat(hexInt >> 16)
  87. let green: CGFloat = CGFloat((hexInt & 0xFF00) >> 8)
  88. let blue: CGFloat = CGFloat(hexInt & 0xFF)
  89. return (red, green, blue)
  90. }
  91. }