UILabel+Extension.swift 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. //
  2. // UILabel+Extension.swift
  3. // QuickSearchLocation
  4. //
  5. // Created by mac on 2024/4/11.
  6. //
  7. import UIKit
  8. extension UILabel {
  9. // MARK: 设置文字
  10. /// 设置文字
  11. /// - Parameter text: 文字内容
  12. /// - Returns: 返回自身
  13. @discardableResult
  14. func text(_ text: String) -> Self {
  15. self.text = text
  16. return self
  17. }
  18. // MARK: 设置文字行数
  19. /// 设置文字行数
  20. /// - Parameter number: 行数
  21. /// - Returns: 返回自身
  22. @discardableResult
  23. func line(_ number: Int) -> Self {
  24. numberOfLines = number
  25. return self
  26. }
  27. // MARK: 设置文本颜色(十六进制字符串)
  28. /// 设置文本颜色(十六进制字符串)
  29. /// - Parameter hex: 十六进制字符串
  30. /// - Returns: 返回自身
  31. @discardableResult
  32. func color(_ hex: String) -> Self {
  33. textColor = UIColor.hexStringColor(hexString: hex)
  34. return self
  35. }
  36. // MARK: 设置字体的大小
  37. /// 设置字体的大小
  38. /// - Parameter fontSize: 字体的大小
  39. /// - Returns: 返回自身
  40. @discardableResult
  41. func font(_ font: UIFont) -> Self {
  42. self.font = font
  43. return self
  44. }
  45. // MARK: 设置字体的大小
  46. /// 设置字体的大小
  47. /// - Parameter fontSize: 字体的大小
  48. /// - Returns: 返回自身
  49. @discardableResult
  50. func font(_ fontSize: CGFloat) -> Self {
  51. font = UIFont.textF(fontSize)
  52. return self
  53. }
  54. // MARK: 设置字体的大小(中等)
  55. /// 设置字体的大小(中等)
  56. /// - Parameter fontSize: 字体的大小
  57. /// - Returns: 返回自身
  58. @discardableResult
  59. func mediumFont(_ fontSize: CGFloat) -> Self {
  60. font = UIFont.textM(fontSize)
  61. return self
  62. }
  63. // MARK: 设置字体的大小(粗体)
  64. /// 设置字体的大小(粗体)
  65. /// - Parameter fontSize: 字体的大小
  66. /// - Returns: 返回自身
  67. @discardableResult
  68. func boldFont(_ fontSize: CGFloat) -> Self {
  69. font = UIFont.textB(fontSize)
  70. return self
  71. }
  72. // MARK: 改变行间距
  73. /// 改变行间距
  74. /// - Parameter space: 行间距大小
  75. func changeLineSpace(space: CGFloat) {
  76. if self.text == nil || self.text == "" {
  77. return
  78. }
  79. let text = self.text
  80. let attributedString = NSMutableAttributedString(string: text!)
  81. let paragraphStyle = NSMutableParagraphStyle()
  82. paragraphStyle.lineSpacing = space
  83. paragraphStyle.alignment = self.textAlignment
  84. attributedString.addAttribute(NSAttributedString.Key.paragraphStyle, value: paragraphStyle, range: .init(location: 0, length: text!.count))
  85. self.attributedText = attributedString
  86. self.sizeToFit()
  87. }
  88. // MARK: label添加中划线
  89. /// label添加中划线
  90. /// - Parameters:
  91. /// - lineValue: value 越大,划线越粗
  92. /// - underlineColor: 中划线的颜色
  93. func centerLineText(lineValue: Int = 1, underlineColor: UIColor = .black) {
  94. guard let content = self.text else {
  95. return
  96. }
  97. let arrText = NSMutableAttributedString(string: content)
  98. arrText.addAttributes([NSAttributedString.Key.strikethroughStyle: lineValue, NSAttributedString.Key.strikethroughColor: underlineColor], range: NSRange(location: 0, length: arrText.length))
  99. self.attributedText = arrText
  100. }
  101. }
  102. extension UILabel {
  103. func setRangeFontText(font: UIFont, range: NSRange) {
  104. let attributedString = self.attributedText?.setRangeFontText(font: font, range: range)
  105. self.attributedText = attributedString
  106. }
  107. func setSpecificTextColor(_ text: String, color: UIColor) {
  108. let attributedString = self.attributedText?.setSpecificTextColor(text, color: color)
  109. self.attributedText = attributedString
  110. }
  111. func setSpecificTextColorFont(_ text: String, color: UIColor, font: UIFont) {
  112. let attributedString = self.attributedText?.setSpecificTextColorFont(text, color: color, font: font)
  113. self.attributedText = attributedString
  114. }
  115. }