// // UILabel+Extension.swift // QuickSearchLocation // // Created by mac on 2024/4/11. // import UIKit extension UILabel { // MARK: 设置文字 /// 设置文字 /// - Parameter text: 文字内容 /// - Returns: 返回自身 @discardableResult func text(_ text: String) -> Self { self.text = text return self } // MARK: 设置文字行数 /// 设置文字行数 /// - Parameter number: 行数 /// - Returns: 返回自身 @discardableResult func line(_ number: Int) -> Self { numberOfLines = number return self } // MARK: 设置文本颜色(十六进制字符串) /// 设置文本颜色(十六进制字符串) /// - Parameter hex: 十六进制字符串 /// - Returns: 返回自身 @discardableResult func color(_ hex: String) -> Self { textColor = UIColor.hexStringColor(hexString: hex) return self } // MARK: 设置字体的大小 /// 设置字体的大小 /// - Parameter fontSize: 字体的大小 /// - Returns: 返回自身 @discardableResult func font(_ font: UIFont) -> Self { self.font = font return self } // MARK: 设置字体的大小 /// 设置字体的大小 /// - Parameter fontSize: 字体的大小 /// - Returns: 返回自身 @discardableResult func font(_ fontSize: CGFloat) -> Self { font = UIFont.textF(fontSize) return self } // MARK: 设置字体的大小(中等) /// 设置字体的大小(中等) /// - Parameter fontSize: 字体的大小 /// - Returns: 返回自身 @discardableResult func mediumFont(_ fontSize: CGFloat) -> Self { font = UIFont.textM(fontSize) return self } // MARK: 设置字体的大小(粗体) /// 设置字体的大小(粗体) /// - Parameter fontSize: 字体的大小 /// - Returns: 返回自身 @discardableResult func boldFont(_ fontSize: CGFloat) -> Self { font = UIFont.textB(fontSize) return self } // MARK: 改变行间距 /// 改变行间距 /// - Parameter space: 行间距大小 func changeLineSpace(space: CGFloat) { if self.text == nil || self.text == "" { return } let text = self.text let attributedString = NSMutableAttributedString(string: text!) let paragraphStyle = NSMutableParagraphStyle() paragraphStyle.lineSpacing = space paragraphStyle.alignment = self.textAlignment attributedString.addAttribute(NSAttributedString.Key.paragraphStyle, value: paragraphStyle, range: .init(location: 0, length: text!.count)) self.attributedText = attributedString self.sizeToFit() } // MARK: label添加中划线 /// label添加中划线 /// - Parameters: /// - lineValue: value 越大,划线越粗 /// - underlineColor: 中划线的颜色 func centerLineText(lineValue: Int = 1, underlineColor: UIColor = .black) { guard let content = self.text else { return } let arrText = NSMutableAttributedString(string: content) arrText.addAttributes([NSAttributedString.Key.strikethroughStyle: lineValue, NSAttributedString.Key.strikethroughColor: underlineColor], range: NSRange(location: 0, length: arrText.length)) self.attributedText = arrText } } extension UILabel { func setRangeFontText(font: UIFont, range: NSRange) { let attributedString = self.attributedText?.setRangeFontText(font: font, range: range) self.attributedText = attributedString } func setSpecificTextColor(_ text: String, color: UIColor) { let attributedString = self.attributedText?.setSpecificTextColor(text, color: color) self.attributedText = attributedString } func setSpecificTextColorFont(_ text: String, color: UIColor, font: UIFont) { let attributedString = self.attributedText?.setSpecificTextColorFont(text, color: color, font: font) self.attributedText = attributedString } }