UILabel+Ex.swift 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. //
  2. // UILabel+Ex.swift
  3. // TSLiveWallpaper
  4. //
  5. // Created by 100Years on 2024/12/20.
  6. //
  7. public extension UILabel {
  8. /// 快速创建 UILabel
  9. /// - Parameters:
  10. /// - text: 文本内容
  11. /// - font: 字体
  12. /// - textColor: 文本颜色
  13. /// - textAlignment: 文本对齐方式
  14. /// - numberOfLines: 行数,默认 1
  15. /// - backgroundColor: 背景颜色
  16. /// - adjustsFontSizeToFitWidth: 是否自适应宽度,默认 false
  17. /// - cornerRadius: 圆角,默认 0
  18. /// - Returns: 配置好的 UILabel 实例
  19. static public func createLabel(text: String? = nil,
  20. font: UIFont? = nil,
  21. textColor: UIColor? = nil,
  22. textAlignment: NSTextAlignment = .left,
  23. numberOfLines: Int = 0,
  24. backgroundColor: UIColor? = nil,
  25. adjustsFontSizeToFitWidth: Bool = false,
  26. cornerRadius: CGFloat = 0) -> UILabel {
  27. let label = UILabel()
  28. label.setUpLabel(
  29. text: text,
  30. font: font,
  31. textColor: textColor,
  32. textAlignment: textAlignment,
  33. numberOfLines: numberOfLines,
  34. backgroundColor: backgroundColor,
  35. adjustsFontSizeToFitWidth: adjustsFontSizeToFitWidth,
  36. cornerRadius: cornerRadius
  37. )
  38. return label
  39. }
  40. public func setUpLabel(text: String? = nil,
  41. font: UIFont? = nil,
  42. textColor: UIColor? = nil,
  43. textAlignment: NSTextAlignment = .left,
  44. numberOfLines: Int = 0,
  45. backgroundColor: UIColor? = nil,
  46. adjustsFontSizeToFitWidth: Bool = false,
  47. cornerRadius: CGFloat = 0) {
  48. let label = self
  49. // 设置文本内容
  50. label.text = text
  51. // 设置字体
  52. if let font = font {
  53. label.font = font
  54. }
  55. // 设置文本颜色
  56. if let textColor = textColor {
  57. label.textColor = textColor
  58. }
  59. // 设置文本对齐方式
  60. label.textAlignment = textAlignment
  61. //阿拉伯语适配
  62. if kIsRTL {
  63. label.semanticContentAttribute = .forceRightToLeft
  64. if textAlignment == .left {
  65. label.textAlignment = .right
  66. }else if textAlignment == .right {
  67. label.textAlignment = .left
  68. }
  69. }
  70. // 设置行数
  71. label.numberOfLines = numberOfLines
  72. // 设置背景颜色
  73. if let backgroundColor = backgroundColor {
  74. label.backgroundColor = backgroundColor
  75. }
  76. // 设置是否自适应宽度
  77. label.adjustsFontSizeToFitWidth = adjustsFontSizeToFitWidth
  78. // 设置圆角
  79. if cornerRadius > 0 {
  80. label.layer.cornerRadius = cornerRadius
  81. label.layer.masksToBounds = true
  82. }
  83. }
  84. /// 设置行间距
  85. /// - Parameters:
  86. /// - lineSpacing: 行间距
  87. /// - alignment: 对齐方式(默认为左对齐)
  88. func setLineSpacing(_ lineSpacing: CGFloat) {
  89. guard let text = self.text else { return }
  90. // 创建 NSMutableAttributedString
  91. let attributedString = NSMutableAttributedString(string: text)
  92. // 创建段落样式
  93. let paragraphStyle = NSMutableParagraphStyle()
  94. paragraphStyle.lineSpacing = lineSpacing
  95. paragraphStyle.alignment = self.textAlignment
  96. // 应用段落样式到整个文本
  97. attributedString.addAttribute(.paragraphStyle, value: paragraphStyle, range: NSRange(location: 0, length: attributedString.length))
  98. // 设置属性文本
  99. self.attributedText = attributedString
  100. }
  101. }