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

iOS swift 带有attributeString的多行文本label的更多相关文章

  1. 使用Autolayout对多行文本Label进行布局,高度不准确的解决办法!

    BUG描述: 今天公司的项目中发现了一个BUG,大概给大家描述一下,tabbleView有一个tableFooterView,这个footView中有一个Label,是多行显示文本,程序用的是Auto ...

  2. iOS - Swift 命令行输入输出

    1.类输出 Swift 语言中类输出方法重: override var description: String{ return String(format: "%@, %@", s ...

  3. iOS Swift 模块练习/swift基础学习

    SWIFT项目练习     SWIFT项目练习2 iOS Swift基础知识代码 推荐:Swift学习使用知识代码软件 0.swift中的宏定义(使用方法代替宏) 一.视图  +控件 1.UIImag ...

  4. 给iOS开发新手送点福利,简述文本属性Attributes的用法

    给iOS开发新手送点福利,简述文本属性Attributes的用法   文本属性Attributes 1.NSKernAttributeName: @10 调整字句 kerning 字句调整 2.NSF ...

  5. sharepoint更新多行文本webparth

    前台 <script> function Copy() { var value = document.getElementById("<%=BodyBox.ClientID ...

  6. 禁止多行文本框textarea拖拽

    禁止多行文本框textarea拖拽: textarea { resize: none; } resize这个是用于元素缩放,它可以取以下几个值: none 默认值 both 允许水平方向及垂直方向缩放 ...

  7. NGUI 3.5教程(二)Label 标签 (Hello world)、多行文本

    写在前面:     本文将创建NGUI的第一个样例.依照编程传统,第一个样例,就是做一个Hello world 显示出来.NGUI.我们用Label来实现 . 欢迎大家纠错.拍砖!原创非常辛苦,如有转 ...

  8. iOS swift的xcworkspace多项目管理(架构思想)

    iOS  swift的xcworkspace多项目管理(架构思想) 技术说明: 今天在这里分享 swift下的 xcworkspace多项目管理(架构思想),能为我们在开发中带来哪些便捷?能为我们对整 ...

  9. ios swift 实现饼状图进度条,swift环形进度条

    ios swift 实现饼状图进度条 // // ProgressControl.swift // L02MyProgressControl // // Created by plter on 7/2 ...

随机推荐

  1. Hack the 21LTR: Scene 1 VM (Boot to Root)

    靶机下载链接: https://www.vulnhub.com/entry/21ltr-scene-1,3/ 主机扫描: ╰─ nmap -p1-65535 -sV -sC -A  192.168.2 ...

  2. FontLab VI for Mac 键盘快捷键

    使用FontLab VI for Mac,您可以创建,打开,修改,绘制,空间,文字,提示和导出桌面,网页,颜色和可变字体.该应用程序是一个全能的字体编辑器,但也支持与其他字体创建工具的数据交换,使其易 ...

  3. jimdb压测踩坑记

    本文记录在jimdb压测过程中遇到的各种小坑,望能够抛砖引玉. 1.压测流量起来后,过了5分钟左右,发现ops突降,大概降了三分之一,然后稳定了下来 大概原因:此种情况,jimdb极有可能某个分片的连 ...

  4. python高阶函数——map/reduce

    python 内置了map()和reduce()函数 1.map()函数 map()函数接收两个参数,一个是函数,一个是可迭代对象Iterable,map将传入的函数依次作用于序列的每一个元素.并把结 ...

  5. Mybatis的PageHelper分页插件的PageInfo的属性参数,成员变量的解释,以及页面模板

    作者:个人微信公众号:程序猿的月光宝盒 //当前页 private int pageNum; //每页的数量 private int pageSize; //当前页的数量 private int si ...

  6. 基于Redis消息的订阅发布应用场景

    目录 基于Redis消息的订阅发布应用场景 1.应用背景 2.困境 2.1 锁表风险 2.2 实时性差 2.3 增加编程复杂性 2.4 实时效果 3.解决方案 3.1 前端传值给服务端 3.2 服务端 ...

  7. Spring 框架下的 JDBC

    Spring JDBC Spring 对JDBC技术规范做了进一步封装,它又叫Spring JDBCTemplate(jdbc模板技术) 纯JDBC:代码清晰的.效率最高.代码是最烦的.    Spr ...

  8. vue如何debugger源码

    在我们阅读vue源码时,一般引用vue的版本都是打包编译后的版本,无法debugger源码,要debugger源码,就需要给代码添加sourcemap,他存储源码与编译后代码的一种对应关系,详细内容可 ...

  9. Geoserver安装

    准备内容 安装环境:win10*64位专业版 安装文件:geoserver-2.15.2 安装步骤 安装JDK 1.安装GeoServer是基于Java的环境,所以需要先装Jdk环境. 2.前往官网下 ...

  10. 拥抱自动化,CODING 2.0 持续集成全新上线

    在文章开始前,做一个小调查,在您的软件项目中集成一行新代码平均需要花多长时间? 15 分钟 一小时 半天 一天及以上 注意这里的集成是指将源码放在一起,并验证源码可以作为一个一致.运行可靠的软件的过程 ...