-
Notifications
You must be signed in to change notification settings - Fork 0
/
StringExtension.swift
140 lines (130 loc) · 5.35 KB
/
StringExtension.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
extension String {
/// 获取 a~z 小写英文字母集合
static func getLowercaseLetters() -> [String] {
let aScalars = "a".unicodeScalars
let aCode = aScalars[aScalars.startIndex].value
let letters: [String] = (0..<26).map { String(UnicodeScalar(aCode + $0)!) }
return letters
}
/// 获取 A~Z 大写英文字母集合
static func getCapitalLetters() -> [String] {
let aScalars = "A".unicodeScalars
let aCode = aScalars[aScalars.startIndex].value
let letters: [String] = (0..<26).map { String(UnicodeScalar(aCode + $0)!) }
return letters
}
/// 获取 0~9 数字集合
static func getNumbers() -> [String] {
let zeroScalars = "0".unicodeScalars
let zeroCode = zeroScalars[zeroScalars.startIndex].value
let numbers: [String] = (0..<10).map { String(UnicodeScalar(zeroCode + $0)!) }
return numbers
}
/// 从颜色编码产生 UIColor 实例,支持 hex/rgba/argb/rgb
func generateUIColor() -> UIColor {
let leftParenCharset: CharacterSet = CharacterSet(charactersIn: "( ")
let commaCharset: CharacterSet = CharacterSet(charactersIn: ", ")
let colorString = self.lowercased()
if colorString.hasPrefix("#")
{
var argb: [UInt] = [255, 0, 0, 0]
let colorString = colorString.unicodeScalars
var length = colorString.count
var index = colorString.startIndex
let endIndex = colorString.endIndex
index = colorString.index(after: index)
length = length - 1
if length == 3 || length == 6 || length == 8
{
var i = length == 8 ? 0 : 1
while index < endIndex
{
var c = colorString[index]
index = colorString.index(after: index)
var val = (c.value >= 0x61 && c.value <= 0x66) ? (c.value - 0x61 + 10) : c.value - 0x30
argb[i] = UInt(val) * 16
if length == 3
{
argb[i] = argb[i] + UInt(val)
}
else
{
c = colorString[index]
index = colorString.index(after: index)
val = (c.value >= 0x61 && c.value <= 0x66) ? (c.value - 0x61 + 10) : c.value - 0x30
argb[i] = argb[i] + UInt(val)
}
i += 1
}
}
return UIColor(red: CGFloat(argb[1]) / 255.0, green: CGFloat(argb[2]) / 255.0, blue: CGFloat(argb[3]) / 255.0, alpha: CGFloat(argb[0]) / 255.0)
}
else if colorString.hasPrefix("rgba")
{
var a: Float = 1.0
var r: Int32 = 0
var g: Int32 = 0
var b: Int32 = 0
let scanner: Scanner = Scanner(string: colorString)
scanner.scanString("rgba", into: nil)
scanner.scanCharacters(from: leftParenCharset, into: nil)
scanner.scanInt32(&r)
scanner.scanCharacters(from: commaCharset, into: nil)
scanner.scanInt32(&g)
scanner.scanCharacters(from: commaCharset, into: nil)
scanner.scanInt32(&b)
scanner.scanCharacters(from: commaCharset, into: nil)
scanner.scanFloat(&a)
return UIColor(
red: CGFloat(r) / 255.0,
green: CGFloat(g) / 255.0,
blue: CGFloat(b) / 255.0,
alpha: CGFloat(a)
)
}
else if colorString.hasPrefix("argb")
{
var a: Float = 1.0
var r: Int32 = 0
var g: Int32 = 0
var b: Int32 = 0
let scanner: Scanner = Scanner(string: colorString)
scanner.scanString("argb", into: nil)
scanner.scanCharacters(from: leftParenCharset, into: nil)
scanner.scanFloat(&a)
scanner.scanCharacters(from: commaCharset, into: nil)
scanner.scanInt32(&r)
scanner.scanCharacters(from: commaCharset, into: nil)
scanner.scanInt32(&g)
scanner.scanCharacters(from: commaCharset, into: nil)
scanner.scanInt32(&b)
return UIColor(
red: CGFloat(r) / 255.0,
green: CGFloat(g) / 255.0,
blue: CGFloat(b) / 255.0,
alpha: CGFloat(a)
)
}
else if colorString.hasPrefix("rgb")
{
var r: Int32 = 0
var g: Int32 = 0
var b: Int32 = 0
let scanner: Scanner = Scanner(string: colorString)
scanner.scanString("rgb", into: nil)
scanner.scanCharacters(from: leftParenCharset, into: nil)
scanner.scanInt32(&r)
scanner.scanCharacters(from: commaCharset, into: nil)
scanner.scanInt32(&g)
scanner.scanCharacters(from: commaCharset, into: nil)
scanner.scanInt32(&b)
return UIColor(
red: CGFloat(r) / 255.0,
green: CGFloat(g) / 255.0,
blue: CGFloat(b) / 255.0,
alpha: 1.0
)
}
return UIColor.clear
}
}