String+Extenstion.swift 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. //
  2. // String+Extenstion.swift
  3. // o2app
  4. //
  5. // Created by 刘振兴 on 2017/8/18.
  6. // Copyright © 2017年 zone. All rights reserved.
  7. //
  8. import Foundation
  9. import UIKit
  10. extension String {
  11. /// 获取扩展名 比如 png gif 等
  12. public var pathExtension: String {
  13. guard let url = URL(string: self) else { return "" }
  14. return url.pathExtension.isEmpty ? "" : url.pathExtension
  15. }
  16. /// 获取文件名称
  17. public var pathFileName: String {
  18. guard let url = URL(string: self) else {
  19. return ""
  20. }
  21. return url.lastPathComponent
  22. }
  23. /// EZSE: Checks if string is empty or consists only of whitespace and newline characters
  24. public var isBlank: Bool {
  25. let trimmed = trimmingCharacters(in: .whitespacesAndNewlines)
  26. return trimmed.isEmpty
  27. }
  28. /// EZSE: split string using a spearator string, returns an array of string
  29. public func split(_ separator: String) -> [String] {
  30. return self.components(separatedBy: separator).filter {
  31. !$0.trim().isEmpty
  32. }
  33. }
  34. /// EZSE: split string with delimiters, returns an array of string
  35. public func split(_ characters: CharacterSet) -> [String] {
  36. return self.components(separatedBy: characters).filter {
  37. !$0.trim().isEmpty
  38. }
  39. }
  40. public func trim(trimNewline: Bool = false) ->String {
  41. if trimNewline {
  42. return self.trimmingCharacters(in: .whitespacesAndNewlines)
  43. }
  44. return self.trimmingCharacters(in: .whitespaces)
  45. }
  46. /// 字符串时间转 Date
  47. ///
  48. /// - Parameter formatter: 字符串时间的格式 yyyy-MM-dd/YYYY-MM-dd/HH:mm:ss/yyyy-MM-dd HH:mm:ss
  49. /// - Returns: Date
  50. func toDate(formatter: String) -> Date {
  51. let dateFormatter = DateFormatter()
  52. dateFormatter.locale = Locale.current
  53. dateFormatter.dateFormat = formatter
  54. let date = dateFormatter.date(from: self)
  55. return date!
  56. }
  57. var length: Int {
  58. return self.count
  59. }
  60. func subString(from: Int, to: Int? = nil) -> String {
  61. if from >= self.length {
  62. return self
  63. }
  64. let startIndex = self.index(self.startIndex, offsetBy: from)
  65. if to == nil {
  66. return String(self[startIndex..<self.endIndex])
  67. }else {
  68. if from >= to! {
  69. return String(self[startIndex..<self.endIndex])
  70. }else {
  71. let endIndex = index(self.startIndex, offsetBy: to!)
  72. return String(self[startIndex..<endIndex])
  73. }
  74. }
  75. }
  76. /// 计算文本的高度
  77. func textHeight(fontSize: CGFloat, width: CGFloat) -> CGFloat {
  78. return self.boundingRect(with: CGSize(width: width, height: CGFloat(MAXFLOAT)), options: .usesLineFragmentOrigin, attributes: [.font: UIFont.systemFont(ofSize: fontSize)], context: nil).size.height
  79. }
  80. // MARK: - URL允许的字符
  81. var urlEscaped: String {
  82. return self.addingPercentEncoding(withAllowedCharacters: .urlHostAllowed) ?? ""
  83. }
  84. var urlEncoded: String {
  85. return self.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) ?? ""
  86. }
  87. func urlEncoding() -> String {
  88. let toSearchword = (self as NSString).addingPercentEncoding(withAllowedCharacters: CharacterSet(charactersIn: #"?!@#$^&%*+,:;='"`<>()[]{}/\|"#).inverted)
  89. return toSearchword as String? ?? ""
  90. }
  91. // MARK:- 获取字符串的CGSize
  92. func getSize(with fontSize: CGFloat) -> CGSize {
  93. let str = self as NSString
  94. let size = CGSize(width: UIScreen.main.bounds.width, height: CGFloat(MAXFLOAT))
  95. return str.boundingRect(with: size, options: .usesLineFragmentOrigin, attributes: [NSAttributedString.Key.font: UIFont.systemFont(ofSize: fontSize)], context: nil).size
  96. }
  97. // MARK: - 根据固定宽度获取字符串在label中的size
  98. func getSizeWithMaxWidth(fontSize:CGFloat, maxWidth: CGFloat) -> CGSize {
  99. let size = CGSize(width: maxWidth, height: CGFloat(MAXFLOAT))
  100. return self.boundingRect(with: size, options: .usesLineFragmentOrigin, attributes: [NSAttributedString.Key.font: UIFont.systemFont(ofSize: fontSize)], context: nil).size
  101. }
  102. // MARK:- 获取文本图片
  103. func getTextImage(_ size:CGSize,textColor tColor:UIColor,backColor bColor:UIColor,textFont tFont:UIFont) -> UIImage? {
  104. let label = UILabel(frame: CGRect(origin:CGPoint(x:0,y:0), size: size))
  105. label.textAlignment = .center
  106. label.textColor = tColor
  107. label.font = tFont
  108. label.text = self
  109. label.backgroundColor = bColor
  110. UIGraphicsBeginImageContextWithOptions(label.frame.size, true, 0)
  111. guard let context = UIGraphicsGetCurrentContext() else { return nil }
  112. label.layer.render(in: context)
  113. let image = UIGraphicsGetImageFromCurrentImageContext()
  114. UIGraphicsEndImageContext()
  115. return image
  116. }
  117. subscript(r: Range<Int>) -> String {
  118. get {
  119. let startIndex = self.index(self.startIndex, offsetBy: r.lowerBound)
  120. let endIndex = self.index(self.startIndex, offsetBy: r.upperBound)
  121. return String(self[startIndex..<endIndex])
  122. }
  123. }
  124. subscript(r: ClosedRange<Int>) -> String {
  125. get {
  126. let startIndex = self.index(self.startIndex, offsetBy: r.lowerBound)
  127. let endIndex = self.index(self.startIndex, offsetBy: r.upperBound)
  128. return String(self[startIndex...endIndex])
  129. }
  130. }
  131. static func randomString(length:Int) -> String {
  132. let charSet = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
  133. var c = charSet.map { String($0) }
  134. var s:String = ""
  135. for _ in (1...length) {
  136. s.append(c[Int(arc4random()) % c.count])
  137. }
  138. return s
  139. }
  140. /// o2 后台统一的des加密
  141. func o2DESEncode() -> String? {
  142. if let encode = desEncrypt(key: O2ConfigInfo.O2_OA_DES_KEY, iv: "12345678", options: (kCCOptionECBMode + kCCOptionPKCS7Padding)) {
  143. print("解密后的字符串:\(encode)")
  144. let first = encode.replacingOccurrences(of: "+", with: "-")
  145. let second = first.replacingOccurrences(of: "/", with: "_")
  146. let token = second.replacingOccurrences(of: "=", with: "")
  147. print("安全替换后的字符串:\(token)")
  148. return token
  149. }else {
  150. print("加密错误")
  151. return nil
  152. }
  153. }
  154. /// o2 后台统一的des解密
  155. func o2DESDecode() -> String? {
  156. return desDecrypt(key: O2ConfigInfo.O2_OA_DES_KEY, iv: "12345678", options: (kCCOptionECBMode + kCCOptionPKCS7Padding))
  157. }
  158. /// DES 加密
  159. func desEncrypt(key:String, iv:String, options:Int = kCCOptionPKCS7Padding) -> String? {
  160. if let keyData = key.data(using: String.Encoding.utf8),
  161. let data = self.data(using: String.Encoding.utf8),
  162. let cryptData = NSMutableData(length: Int((data.count)) + kCCBlockSizeDES) {
  163. let keyLength = size_t(kCCKeySizeDES)
  164. let operation: CCOperation = UInt32(kCCEncrypt)
  165. let algoritm: CCAlgorithm = UInt32(kCCAlgorithmDES)
  166. let options: CCOptions = UInt32(options)
  167. var numBytesEncrypted :size_t = 0
  168. let cryptStatus = CCCrypt(operation,
  169. algoritm,
  170. options,
  171. (keyData as NSData).bytes, keyLength,
  172. iv,
  173. (data as NSData).bytes, data.count,
  174. cryptData.mutableBytes, cryptData.length,
  175. &numBytesEncrypted)
  176. if UInt32(cryptStatus) == UInt32(kCCSuccess) {
  177. cryptData.length = Int(numBytesEncrypted)
  178. // let base64cryptString = cryptData.base64EncodedString(options: .lineLength64Characters)
  179. let base64cryptString = cryptData.base64EncodedString()
  180. return base64cryptString
  181. }
  182. else {
  183. return nil
  184. }
  185. }
  186. return nil
  187. }
  188. /// DES 解密
  189. func desDecrypt(key:String, iv:String, options:Int = kCCOptionPKCS7Padding) -> String? {
  190. if let keyData = key.data(using: String.Encoding.utf8),
  191. let data = NSData(base64Encoded: self, options: .ignoreUnknownCharacters),
  192. let cryptData = NSMutableData(length: Int((data.length)) + kCCBlockSizeDES) {
  193. let keyLength = size_t(kCCKeySizeDES)
  194. let operation: CCOperation = UInt32(kCCDecrypt)
  195. let algoritm: CCAlgorithm = UInt32(kCCAlgorithmDES)
  196. let options: CCOptions = UInt32(options)
  197. var numBytesEncrypted :size_t = 0
  198. let cryptStatus = CCCrypt(operation,
  199. algoritm,
  200. options,
  201. (keyData as NSData).bytes, keyLength,
  202. iv,
  203. data.bytes, data.length,
  204. cryptData.mutableBytes, cryptData.length,
  205. &numBytesEncrypted)
  206. if UInt32(cryptStatus) == UInt32(kCCSuccess) {
  207. cryptData.length = Int(numBytesEncrypted)
  208. let unencryptedMessage = String(data: cryptData as Data, encoding:String.Encoding.utf8)
  209. return unencryptedMessage
  210. }
  211. else {
  212. return nil
  213. }
  214. }
  215. return nil
  216. }
  217. // MARK:- 获取帐号中的中文名称
  218. func getChinaName() -> String{
  219. let userName = self
  220. var strTemp = ""
  221. if !userName.isBlank{
  222. let userNameSplit = userName.split("@");
  223. if strTemp == "" {
  224. strTemp = userNameSplit[0]
  225. }else{
  226. strTemp = strTemp + "," + userNameSplit[0]
  227. }
  228. print(strTemp)
  229. }
  230. return strTemp
  231. }
  232. }