class AttributeStringGenerator {

var attributeString: NSMutableAttributedString!

var lineSpacing: CGFloat = 2

init() {

attributeString = NSMutableAttributedString()

}

func reset() {

attributeString = NSMutableAttributedString()

}

/// 添加空行

func addEmptyLine(height: CGFloat = 12) {

let font = UIFont.systemFont(ofSize: height)

let attr = NSAttributedString(string: "\n", attributes:  [NSAttributedString.Key.font:font])

attributeString.append(attr)

}

/// 加一行文字

///

/// - Parameters:

///   - string: 要加的文字

///   - alignment: 对齐方式

///   - color: 颜色

///   - font: 字体

///   - isLastLine: 是否最后一行,最后一行不加换行

func addLine(string: String, alignment: NSTextAlignment, color: UIColor = UIColor.white, font: UIFont = UIFont.systemFont(ofSize: 12), isLastLine: Bool = false) {

let para = NSMutableParagraphStyle()

para.alignment = alignment

para.lineSpacing = lineSpacing

//如果是最后一行,不需要加 \n

var s = string

if !isLastLine {

s += "\n"

}

let attr = NSAttributedString(string: s, attributes:

[NSAttributedString.Key.paragraphStyle:para,

NSAttributedString.Key.foregroundColor:color,

NSAttributedString.Key.font:font])

attributeString.append(attr)

}

/// 添加文字居中带图标的行

///

/// - Parameters:

///   - icon: 图标

///   - string: 文字

///   - size: 图标大小

///   - color: 文字颜色

///   - font: 字体

///   - isLastLine: 是否最后一行

func addLineWith(icon: UIImage,string: String, size: CGSize, color: UIColor = UIColor.white, font: UIFont = UIFont.systemFont(ofSize: 18), isLastLine: Bool = true) {

let para = NSMutableParagraphStyle()

para.alignment = .center

para.lineSpacing = lineSpacing

//如果是最后一行,不需要加 \n

var s = string

if !isLastLine {

s += "\n"

}

let att = NSTextAttachment(data: nil, ofType: nil)

att.image = icon

att.bounds = CGRect(x: 0, y: -font.pointSize/10, width: size.width, height: size.height)

let attr = NSMutableAttributedString(attributedString: NSAttributedString(attachment: att))

attr.append(NSMutableAttributedString(string: " " + s, attributes:

[NSAttributedString.Key.foregroundColor:color,

NSAttributedString.Key.font:font]))

attr.addAttribute(NSAttributedString.Key.paragraphStyle, value: para, range: NSMakeRange(0, attr.length))

attributeString.append(attr)

}

/// 添加一行文字,分左中右三部分, 左边的左对齐, 中间的右对齐,可以设置通过某符号对齐,比如 ":", 右边的可以自行设置对齐方式

///

/// - Parameters:

///   - leftString: 左边的文字

///   - leftColor: 左边文字的颜色

///   - leftFont: 左边文字字体

///   - centerString: 中间的文字

///   - centerPosition: 中间文字的位置

///   - centerColor: 中间文字颜色

///   - centerFont: 中间文字字体

///   - alignString: 中间文字对齐的符号

///   - rightString: 右边的文字

///   - rightPosition: 右边文字的位置

///   - rightColor: 右边文字颜色

///   - rightFont: 右边文字字体

///   - alignment: 右边文字对齐方式

func addLine(leftString: String?, leftColor: UIColor = UIColor.white, leftFont: UIFont = UIFont.systemFont(ofSize: 12),

centerString: String? = nil,centerPosition:CGFloat = 0, centerColor: UIColor = UIColor.white, centerFont: UIFont = UIFont.systemFont(ofSize: 12), alignString: String = "",

rightString: String?,rightPosition:CGFloat = 0, rightColor: UIColor = UIColor.white, rightFont: UIFont = UIFont.systemFont(ofSize: 12), alignment: NSTextAlignment = .right,

isLastLine: Bool = false) {

var string = ""

var leftRange: NSRange

var centerRange: NSRange

var rightRange: NSRange

if let left = leftString {

string = string + left + "\t"

//特殊字符.count算出的长度不对,需要改成 lengthOfBytes(using: String.Encoding.unicode)/2

leftRange = NSMakeRange(0, string.lengthOfBytes(using: String.Encoding.unicode)/2)

} else {

string += "\t"

leftRange = NSMakeRange(0, 1)

}

if let center = centerString {

string = string + center + "\t"

centerRange = NSMakeRange(leftRange.length, center.lengthOfBytes(using: String.Encoding.unicode)/2 + 1)

} else {

//string += "\t"

centerRange = NSMakeRange(leftRange.length, 0)

}

if let right = rightString {

if isLastLine {

string = string + right

rightRange = NSMakeRange(leftRange.length + centerRange.length, right.lengthOfBytes(using: String.Encoding.unicode)/2)

} else {

string = string + right + "\n"

rightRange = NSMakeRange(leftRange.length + centerRange.length, right.lengthOfBytes(using: String.Encoding.unicode)/2 + 1)

}

} else {

if isLastLine {

string += "\n"

rightRange = NSMakeRange(leftRange.length + centerRange.length, 1)

} else {

rightRange = NSMakeRange(leftRange.length + centerRange.length, 0)

}

}

let para = NSMutableParagraphStyle()

para.lineSpacing = lineSpacing

let align = NSCharacterSet(charactersIn: alignString)

let t1 = NSTextTab(textAlignment: .right, location: centerPosition, options: [NSTextTab.OptionKey.columnTerminators:align])

let t2 = NSTextTab(textAlignment: alignment, location: rightPosition, options: [:])

para.tabStops = [t1,t2]

if centerString == nil {

para.tabStops = [t2]

}

let attr = NSMutableAttributedString(string: string)

attr.addAttribute(NSAttributedString.Key.paragraphStyle, value: para, range: NSMakeRange(0, string.count))

attr.addAttributes([NSAttributedString.Key.foregroundColor:leftColor, NSAttributedString.Key.font:leftFont], range: leftRange)

attr.addAttributes([NSAttributedString.Key.foregroundColor:centerColor, NSAttributedString.Key.font:centerFont], range: centerRange)

attr.addAttributes([NSAttributedString.Key.foregroundColor:rightColor, NSAttributedString.Key.font:rightFont], range: rightRange)

attributeString.append(attr)

}

func getHeight(width:CGFloat) -> CGFloat {

let options : NSStringDrawingOptions = [.usesLineFragmentOrigin, .usesFontLeading]

let bounds = attributeString.boundingRect(with: CGSize(width: width, height: 99999), options: options, context: nil)

return bounds.height

}

}

最后将attributeString赋值给label

最新文章

  1. JAVA开发中遇到的小白点
  2. HTML input-file 上传类型控制
  3. [Spring框架]Spring开发实例: XML+注解.
  4. 用tcc遇到的一个大坑
  5. java笔记--关于线程死锁
  6. list map vector set 常用函数列表
  7. LightOJ 1140 How Many Zeroes
  8. LINQ标准查询操作符(二)——Join、GroupJoin、GroupBy、Concat、
  9. WebAPI GET和POST请求的几种方(转发)
  10. 非刚性图像配准 matlab简单示例 demons算法
  11. CSS实现背景透明/半透明效果的方法
  12. Generative Learning algorithms
  13. Undefined symbols for architecture arm64(其cpu架构)
  14. python中的张量运算(tensor)
  15. #个人博客作业week2——关于代码规范的个人观点
  16. Jira 添加自定义字段
  17. 20145332卢鑫 WEB基础
  18. php 7.2 安装 mcrypt 扩展: mcrypt 扩展从 php 7.1.0 开始废弃;自 php 7.2.0 起,会移到 pecl
  19. 02.ZooKeeper的Java客户端使用
  20. Lecture notes of Mathematical analysis

热门文章

  1. vue-其他
  2. Jenkins工程中SQL语句执行的方法
  3. .net上传文件,大文件及下载方式汇总(转)
  4. Ajax之处理不同格式的JSON数据
  5. C#通过反射调用类及方法
  6. 获取本机的IP地址
  7. alpine制作jdk、jre镜像、自定义镜像上传阿里云
  8. angular cli 使用echarts
  9. SQL实用技巧:如何判断一个值是否为数字的方法
  10. 从0系统学Android--3.1编写UI界面