import UIKit import SnapKit extension UILabel { /// 快速初始化UILabel 包含默认参数,初始化过程可以删除部分默认参数简化方法 /// - Parameters: /// - title: 内容 /// - titleColor: 内容颜色 /// - font: 字体大小 /// - textAlignment: 文本对齐方式 /// - lineSpace: 行间距 /// - wordSpace: 字间距 /// - supView: 被添加视图 /// - result: label对象 /// - snapKitMaker: 约束 /// - Returns: 返回UILabel对象 @discardableResult public static func reloadCustemSpacingLabel(reload title: NSString = "", titleColor: UIColor = .black, font: CGFloat = 14, textAlignment: NSTextAlignment = .left, lineSpace: CGFloat = 0, wordSpace: CGFloat = 0, supView: UIView? = nil, result: ((_ label: UILabel) -> Void)? = nil, snapKitMaker: ((ConstraintMaker) -> Void)? = nil) -> UILabel { let label = UILabel() label.text = title as String? label.textColor = titleColor label.font = UIFont.systemFont(ofSize: font) label.lineBreakMode = .byTruncatingTail label.numberOfLines = 0 let attributedString = NSMutableAttributedString(string: title as String) let paragraphStyle = NSMutableParagraphStyle() paragraphStyle.lineSpacing = lineSpace /** LineSpace 行间距 */ attributedString.addAttribute(NSAttributedString.Key.paragraphStyle, value: paragraphStyle, range: NSMakeRange(0, attributedString.length)) /** WordSpace 字间距 */ attributedString.addAttribute(NSAttributedString.Key.kern, value: wordSpace, range: NSRange(location: 0, length: attributedString.length)) label.attributedText = attributedString label.sizeToFit() label.textAlignment = textAlignment guard let sv = supView, let lb = result, let mk = snapKitMaker else { return label } sv.addSubview(label) label.snp.makeConstraints { make in mk(make) } lb(label) return label } /// 创建单个颜色、字体修改的富文本label /// - Parameters: /// - title: 内容 /// - titleColor: 内容颜色 /// - font: 字体大小 /// - textAlignment: 文本对齐方式 /// - lineSpace: 行间距 /// - wordSpace: 字间距 /// - changeText: 改变内容 /// - changeColor: 改变内容颜色 /// - changeFont: 改变内容字体大小 /// - supView: 被添加视图 /// - result: label对象 /// - snapKitMaker: 约束 /// - Returns: 返回UILabel对象 @discardableResult public static func reloadAttributedSingleCustemSpacingLabel(reload title: NSString = "", titleColor: UIColor = .black, font: CGFloat = 14, textAlignment: NSTextAlignment = .left, lineSpace: CGFloat = 0, wordSpace: CGFloat = 0, changeText: String = "", changeColor: UIColor = .black, changeFont: CGFloat = 14, supView: UIView? = nil, result: ((_ label: UILabel) -> Void)? = nil, snapKitMaker: ((ConstraintMaker) -> Void)? = nil) -> UILabel { let label = UILabel() label.text = title as String? label.textColor = titleColor label.font = UIFont.systemFont(ofSize: font) label.lineBreakMode = .byTruncatingTail label.numberOfLines = 0 let attributedString = NSMutableAttributedString(string: title as String) let paragraphStyle = NSMutableParagraphStyle() paragraphStyle.lineSpacing = lineSpace /** LineSpace 行间距 */ attributedString.addAttribute(NSAttributedString.Key.paragraphStyle, value: paragraphStyle, range: NSMakeRange(0, attributedString.length)) /** WordSpace 字间距 */ attributedString.addAttribute(NSAttributedString.Key.kern, value: wordSpace, range: NSRange(location: 0, length: attributedString.length)) /** 改变字体颜色及字体大小 */ let range: NSRange = (title as NSString).range(of: changeText as String) attributedString.addAttributes([NSAttributedString.Key.foregroundColor: changeColor, NSAttributedString.Key.font: UIFont.systemFont(ofSize: changeFont)], range: range) label.attributedText = attributedString label.sizeToFit() label.textAlignment = textAlignment guard let sv = supView, let lb = result, let mk = snapKitMaker else { return label } sv.addSubview(label) label.snp.makeConstraints { make in mk(make) } lb(label) return label } /// 创建多个颜色、字体修改的富文本label /// - Parameters: /// - title: 内容 /// - titleColor: 内容颜色 /// - font: 字体大小 /// - textAlignment: 文本对齐方式 /// - lineSpace: 行间距 /// - wordSpace: 字间距 /// - changeText: 改变内容数组 /// - changeColor: 改变内容颜色 /// - changeFont: 改变内容字体大小 /// - supView: 被添加视图 /// - result: label对象 /// - snapKitMaker: 约束 /// - Returns: 返回UILabel对象 @discardableResult public static func reloadAttributedMultipleCustemSpacingLabel(reload title: NSString = "", titleColor: UIColor = .black, font: CGFloat = 14, textAlignment: NSTextAlignment = .left, lineSpace: CGFloat = 0, wordSpace: CGFloat = 0, changeText: [String] = [], changeColor: UIColor = .black, changeFont: CGFloat = 14, supView: UIView? = nil, result: ((_ label: UILabel) -> Void)? = nil, snapKitMaker: ((ConstraintMaker) -> Void)? = nil) -> UILabel { let label = UILabel() label.text = title as String? label.textColor = titleColor label.font = UIFont.systemFont(ofSize: font) label.lineBreakMode = .byTruncatingTail label.numberOfLines = 0 let attributedString = NSMutableAttributedString(string: title as String) let paragraphStyle = NSMutableParagraphStyle() paragraphStyle.lineSpacing = lineSpace /** LineSpace 行间距 */ attributedString.addAttribute(NSAttributedString.Key.paragraphStyle, value: paragraphStyle, range: NSMakeRange(0, attributedString.length)) /** WordSpace 字间距 */ attributedString.addAttribute(NSAttributedString.Key.kern, value: wordSpace, range: NSRange(location: 0, length: attributedString.length)) /** 改变字体颜色及字体大小 */ for subStr in changeText { let range: NSRange = (title as NSString).range(of: subStr as String) attributedString.addAttributes([NSAttributedString.Key.foregroundColor: changeColor, NSAttributedString.Key.font: UIFont.systemFont(ofSize: changeFont)], range: range) } label.attributedText = attributedString label.sizeToFit() label.textAlignment = textAlignment guard let sv = supView, let lb = result, let mk = snapKitMaker else { return label } sv.addSubview(label) label.snp.makeConstraints { make in mk(make) } lb(label) return label } /// 获取高度 /// - Parameters: /// - titie: 内容 /// - font: 字体大小 /// - width: 宽度 /// - Returns: 返回高度 public static func reloadCustemLabelHeight(reload titie: NSString, font: UIFont, width: CGFloat) -> CGFloat { let string: NSString = titie as NSString let origin = NSStringDrawingOptions.usesLineFragmentOrigin let lead = NSStringDrawingOptions.usesFontLeading let ssss = NSStringDrawingOptions.usesDeviceMetrics let rect = string.boundingRect(with: CGSize(width: width, height: 0), options: [origin, lead, ssss], attributes: [NSAttributedString.Key.font: font], context: nil) let height: CGFloat = rect.height + 10 return height } /// 获取宽度 /// - Parameters: /// - titie: 内容 /// - font: 字体大小 /// - height: 高度 /// - Returns: 返回宽度 public static func reloadCustemLabelWidth(reload titie: NSString, font: UIFont, height: CGFloat) -> CGFloat { let string: NSString = titie as NSString let origin = NSStringDrawingOptions.usesLineFragmentOrigin let lead = NSStringDrawingOptions.usesFontLeading let rect = string.boundingRect(with: CGSize(width: 0, height: height), options: [origin, lead], attributes: [NSAttributedString.Key.font: font], context: nil) let width: CGFloat = rect.width + 10 return width } /// 改变文字大小及颜色 /// - Parameters: /// - totalText: 总内容 /// - conversion: 改变内容 /// - font: 改变字体大小 /// - color: 改变字体颜色 /// - Returns: 转换好的字符串 private class func reloadSingleChangeFontSizeAndColor(totalText: String, changeText conversion: String, changeFont font: UIFont, changeColor color: UIColor) -> NSMutableAttributedString { let range: NSRange = (totalText as NSString).range(of: conversion as String) let str = NSMutableAttributedString(string: totalText as String) str.addAttribute(NSAttributedString.Key.foregroundColor, value: color, range: range) str.addAttribute(NSAttributedString.Key.font, value: font, range: range) return str } /// 改变文字大小及颜色 /// - Parameters: /// - totalText: 总内容 /// - conversionArr: 改变内容数组 /// - font: 改变字体大小 /// - color: 改变字体颜色 /// - Returns: 转换好的字符串 private class func reloadMultipleChangeFontSizeAndColor(totalText: String, changeText conversionArr: NSArray, withFont font: UIFont, withColor color: UIColor) -> NSMutableAttributedString { let str = NSMutableAttributedString(string: totalText as String) for subStr in conversionArr { let range: NSRange = (totalText as NSString).range(of: subStr as! String) str.addAttribute(NSAttributedString.Key.foregroundColor, value: color, range: range) str.addAttribute(NSAttributedString.Key.font, value: font, range: range) } return str } }
发表评论