String+Extension.swift 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. //
  2. // String+Extension.swift
  3. // QuickSearchLocation
  4. //
  5. // Created by Destiny on 2024/4/15.
  6. //
  7. import Foundation
  8. import UIKit
  9. // MARK: - 五、字符串UI的处理
  10. extension String {
  11. // MARK: 9.1、判断是否全是空白,包括空白字符和换行符号,长度为0返回true
  12. /// 判断是否全是空白,包括空白字符和换行符号,长度为0返回true
  13. public var isBlank: Bool {
  14. return self.trimmingCharacters(in: NSCharacterSet.whitespacesAndNewlines) == ""
  15. }
  16. // MARK: 5.1、对字符串(多行)指定出字体大小和最大的 Size,获取 (Size)
  17. /// 对字符串(多行)指定出字体大小和最大的 Size,获取展示的 Size
  18. /// - Parameters:
  19. /// - font: 字体大小
  20. /// - size: 字符串的最大宽和高
  21. /// - Returns: 按照 font 和 Size 的字符的Size
  22. public func rectSize(font: UIFont, size: CGSize) -> CGSize {
  23. let attributes = [NSAttributedString.Key.font: font]
  24. /**
  25. usesLineFragmentOrigin: 整个文本将以每行组成的矩形为单位计算整个文本的尺寸
  26. usesFontLeading:
  27. usesDeviceMetrics:
  28. @available(iOS 6.0, *)
  29. truncatesLastVisibleLine:
  30. */
  31. let option = NSStringDrawingOptions.usesLineFragmentOrigin
  32. let rect: CGRect = self.boundingRect(with: size, options: option, attributes: attributes, context: nil)
  33. return rect.size
  34. }
  35. // MARK: 5.2、对字符串(多行)指定字体及Size,获取 (高度)
  36. /// 对字符串指定字体及Size,获取 (高度)
  37. /// - Parameters:
  38. /// - font: 字体的大小
  39. /// - size: 字体的size
  40. /// - Returns: 返回对应字符串的高度
  41. public func rectHeight(font: UIFont, size: CGSize) -> CGFloat {
  42. return rectSize(font: font, size: size).height
  43. }
  44. // MARK: 5.3、对字符串(多行)指定字体及Size,获取 (宽度)
  45. /// 对字符串指定字体及Size,获取 (宽度)
  46. /// - Parameters:
  47. /// - font: 字体的大小
  48. /// - size: 字体的size
  49. /// - Returns: 返回对应字符串的宽度
  50. public func rectWidth(font: UIFont, size: CGSize) -> CGFloat {
  51. return rectSize(font: font, size: size).width
  52. }
  53. // MARK: 5.4、对字符串(单行)指定字体,获取 (Size)
  54. /// 对字符串(单行)指定字体,获取 (Size)
  55. /// - Parameter font: 字体的大小
  56. /// - Returns: 返回单行字符串的 size
  57. public func singleLineSize(font: UIFont) -> CGSize {
  58. let attrs = [NSAttributedString.Key.font: font]
  59. return self.size(withAttributes: attrs as [NSAttributedString.Key: Any])
  60. }
  61. // MARK: 5.5、对字符串(单行)指定字体,获取 (width)
  62. /// 对字符串(单行)指定字体,获取 (width)
  63. /// - Parameter font: 字体的大小
  64. /// - Returns: 返回单行字符串的 width
  65. public func singleLineWidth(font: UIFont) -> CGFloat {
  66. let attrs = [NSAttributedString.Key.font: font]
  67. return self.size(withAttributes: attrs as [NSAttributedString.Key: Any]).width
  68. }
  69. // MARK: 5.6、对字符串(单行)指定字体,获取 (Height)
  70. /// 对字符串(单行)指定字体,获取 (height)
  71. /// - Parameter font: 字体的大小
  72. /// - Returns: 返回单行字符串的 height
  73. public func singleLineHeight(font: UIFont) -> CGFloat {
  74. let attrs = [NSAttributedString.Key.font: font]
  75. return self.size(withAttributes: attrs as [NSAttributedString.Key: Any]).height
  76. }
  77. // MARK: 5.7、字符串通过 label 根据高度&字体 —> Size
  78. /// 字符串通过 label 根据高度&字体 ——> Size
  79. /// - Parameters:
  80. /// - height: 字符串最大的高度
  81. /// - font: 字体大小
  82. /// - Returns: 返回Size
  83. public func sizeAccording(width: CGFloat, height: CGFloat = CGFloat(MAXFLOAT), font: UIFont) -> CGSize {
  84. if self.isBlank {return CGSize(width: 0, height: 0)}
  85. let rect = CGRect(x: 0, y: 0, width: width, height: height)
  86. let label = UILabel(frame: rect).font(font).text(self).line(0)
  87. return label.sizeThatFits(rect.size)
  88. }
  89. // MARK: 5.8、字符串通过 label 根据高度&字体 —> Width
  90. /// 字符串通过 label 根据高度&字体 ——> Width
  91. /// - Parameters:
  92. /// - height: 字符串最大高度
  93. /// - font: 字体大小
  94. /// - Returns: 返回宽度大小
  95. public func widthAccording(width: CGFloat, height: CGFloat = CGFloat(MAXFLOAT), font: UIFont) -> CGFloat {
  96. if self.isBlank {return 0}
  97. let rect = CGRect(x: 0, y: 0, width: width, height: height)
  98. let label = UILabel(frame: rect).font(font).text(self).line(0)
  99. return label.sizeThatFits(rect.size).width
  100. }
  101. // MARK: 5.9、字符串通过 label 根据宽度&字体 —> height
  102. /// 字符串通过 label 根据宽度&字体 ——> height
  103. /// - Parameters:
  104. /// - width: 字符串最大宽度
  105. /// - font: 字体大小
  106. /// - Returns: 返回高度大小
  107. public func heightAccording(width: CGFloat, height: CGFloat = CGFloat(MAXFLOAT), font: UIFont) -> CGFloat {
  108. if self.isBlank {return 0}
  109. let rect = CGRect(x: 0, y: 0, width: width, height: height)
  110. let label = UILabel(frame: rect).font(font).text(self).line(0)
  111. return label.sizeThatFits(rect.size).height
  112. }
  113. // MARK: 5.10、字符串根据宽度 & 字体 & 行间距 —> Size
  114. /// 字符串根据宽度 & 字体 & 行间距 ——> Size
  115. /// - Parameters:
  116. /// - width: 字符串最大的宽度
  117. /// - heiht: 字符串最大的高度
  118. /// - font: 字体的大小
  119. /// - lineSpacing: 行间距
  120. /// - Returns: 返回对应的size
  121. public func sizeAccording(width: CGFloat, height: CGFloat = CGFloat(MAXFLOAT), font: UIFont, lineSpacing: CGFloat) -> CGSize {
  122. if self.isBlank {return CGSize(width: 0, height: 0)}
  123. let rect = CGRect(x: 0, y: 0, width: width, height: CGFloat(MAXFLOAT))
  124. let label = UILabel(frame: rect).font(font).text(self).line(0)
  125. let attrStr = NSMutableAttributedString(string: self)
  126. let paragraphStyle = NSMutableParagraphStyle()
  127. paragraphStyle.lineSpacing = lineSpacing
  128. attrStr.addAttribute(NSAttributedString.Key.paragraphStyle, value: paragraphStyle, range: NSMakeRange(0, self.count))
  129. label.attributedText = attrStr
  130. return label.sizeThatFits(rect.size)
  131. }
  132. // MARK: 5.11、字符串根据宽度 & 字体 & 行间距 —> width
  133. /// 字符串根据宽度 & 字体 & 行间距 ——> width
  134. /// - Parameters:
  135. /// - width: 字符串最大的宽度
  136. /// - heiht: 字符串最大的高度
  137. /// - font: 字体的大小
  138. /// - lineSpacing: 行间距
  139. /// - Returns: 返回对应的 width
  140. public func widthAccording(width: CGFloat = CGFloat(MAXFLOAT), height: CGFloat, font: UIFont, lineSpacing: CGFloat) -> CGFloat {
  141. if self.isBlank {return 0}
  142. let rect = CGRect(x: 0, y: 0, width: width, height: height)
  143. let label = UILabel(frame: rect).font(font).text(self).line(0)
  144. let attrStr = NSMutableAttributedString(string: self)
  145. let paragraphStyle = NSMutableParagraphStyle()
  146. paragraphStyle.lineSpacing = lineSpacing
  147. attrStr.addAttribute(NSAttributedString.Key.paragraphStyle, value: paragraphStyle, range: NSMakeRange(0, self.count))
  148. label.attributedText = attrStr
  149. return label.sizeThatFits(rect.size).width
  150. }
  151. // MARK: 5.12、字符串根据宽度 & 字体 & 行间距 —> height
  152. /// 字符串根据宽度 & 字体 & 行间距 ——> height
  153. /// - Parameters:
  154. /// - width: 字符串最大的宽度
  155. /// - heiht: 字符串最大的高度
  156. /// - font: 字体的大小
  157. /// - lineSpacing: 行间距
  158. /// - Returns: 返回对应的 height
  159. public func heightAccording(width: CGFloat, height: CGFloat = CGFloat(MAXFLOAT), font: UIFont, lineSpacing: CGFloat) -> CGFloat {
  160. if self.isBlank {return 0}
  161. let rect = CGRect(x: 0, y: 0, width: width, height: height)
  162. let label = UILabel(frame: rect).font(font).text(self).line(0)
  163. let attrStr = NSMutableAttributedString(string: self)
  164. let paragraphStyle = NSMutableParagraphStyle()
  165. paragraphStyle.lineSpacing = lineSpacing
  166. attrStr.addAttribute(NSAttributedString.Key.paragraphStyle, value: paragraphStyle, range: NSMakeRange(0, self.count))
  167. label.attributedText = attrStr
  168. return label.sizeThatFits(rect.size).height
  169. }
  170. }
  171. // MARK: - Base64 编解码
  172. extension String {
  173. var decode: String {
  174. get {
  175. guard let decodeResult = self.base64String(encode: false) else { return "" }
  176. return decodeResult
  177. }
  178. }
  179. // Base64 编解码
  180. /// Base64 编解码
  181. /// - Parameter encode: true:编码 false:解码
  182. /// - Returns: 编解码结果
  183. func base64String(encode: Bool) -> String? {
  184. guard encode else {
  185. // 1.解码
  186. guard let decryptionData = Data(base64Encoded: self, options: .ignoreUnknownCharacters) else {
  187. return nil
  188. }
  189. return String(data: decryptionData, encoding: .utf8)
  190. }
  191. // 2.编码
  192. guard let codingData = self.data(using: .utf8) else {
  193. return nil
  194. }
  195. return codingData.base64EncodedString()
  196. }
  197. // func decode() -> String? {
  198. //
  199. // return self.base64String(encode: false)
  200. // }
  201. }