iOS swift 带有attributeString的多行文本label
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的更多相关文章
- 使用Autolayout对多行文本Label进行布局,高度不准确的解决办法!
BUG描述: 今天公司的项目中发现了一个BUG,大概给大家描述一下,tabbleView有一个tableFooterView,这个footView中有一个Label,是多行显示文本,程序用的是Auto ...
- iOS - Swift 命令行输入输出
1.类输出 Swift 语言中类输出方法重: override var description: String{ return String(format: "%@, %@", s ...
- iOS Swift 模块练习/swift基础学习
SWIFT项目练习 SWIFT项目练习2 iOS Swift基础知识代码 推荐:Swift学习使用知识代码软件 0.swift中的宏定义(使用方法代替宏) 一.视图 +控件 1.UIImag ...
- 给iOS开发新手送点福利,简述文本属性Attributes的用法
给iOS开发新手送点福利,简述文本属性Attributes的用法 文本属性Attributes 1.NSKernAttributeName: @10 调整字句 kerning 字句调整 2.NSF ...
- sharepoint更新多行文本webparth
前台 <script> function Copy() { var value = document.getElementById("<%=BodyBox.ClientID ...
- 禁止多行文本框textarea拖拽
禁止多行文本框textarea拖拽: textarea { resize: none; } resize这个是用于元素缩放,它可以取以下几个值: none 默认值 both 允许水平方向及垂直方向缩放 ...
- NGUI 3.5教程(二)Label 标签 (Hello world)、多行文本
写在前面: 本文将创建NGUI的第一个样例.依照编程传统,第一个样例,就是做一个Hello world 显示出来.NGUI.我们用Label来实现 . 欢迎大家纠错.拍砖!原创非常辛苦,如有转 ...
- iOS swift的xcworkspace多项目管理(架构思想)
iOS swift的xcworkspace多项目管理(架构思想) 技术说明: 今天在这里分享 swift下的 xcworkspace多项目管理(架构思想),能为我们在开发中带来哪些便捷?能为我们对整 ...
- ios swift 实现饼状图进度条,swift环形进度条
ios swift 实现饼状图进度条 // // ProgressControl.swift // L02MyProgressControl // // Created by plter on 7/2 ...
随机推荐
- .Net Core WebApi 模型验证无效时报400
问题 模型验证无效时,没有进入到接口里,而是直接报400 Bad Request,非常不友好. 环境 SDK:.Net Core 2.2.401 开发工具:VS2017 step 1 创建接口 /// ...
- 数据库查询 - DataTable转Entity类型数据
当使用Sql语句查询数据库,返回DataSet数据集. DataSet转化为数据列表,可以通过映射方式直接返回Entity数据列表 新建一个特性类,用于数据库列表列名称映射 LinqToDB提供了一个 ...
- js的常用场景效果
转自https://www.cnblogs.com/tangdiao/p/9481681.html 1.checkbox的使用场景,学习之后就是购物车页面的自动计算的上手示例. 做成给checkbox ...
- ES6知识点脑图
点击左键 => 拖拽图片 => 新标签页查看图片 => 放大拖拽查阅
- vue--CRUD
1. Create this.$http.post("http://localhost:3000/users",newCustomer).then(function (respon ...
- 数据存储与访问之——SharedPreferences
使用SharedPreferences(保存用户偏好参数)保存数据, 当我们的应用想要保存用户的一些偏好参数,比如是否自动登陆,是否记住账号密码,是否在Wifi下才能 联网等相关信息,如果使用数据库的 ...
- 3、netty第二个例子,使用netty建立客户端,与服务端通讯
第一个例子中,建立了http的服务器端,可以直接使用curl命令,或者浏览器直接访问. 在第二个例子中,建立一个netty的客户端来主动发送请求,模拟浏览器发送请求. 这里先启动服务端,再启动客户端, ...
- i2c中start和restart的区别【转】
有的硬件芯片提供了一个个寄存器,供我们很好的操作i2c,但是,在用的时候,我们是不知道他到地是怎么操作的,下边,我就探讨下i2c中的start和restart的区别. start是在scl是高电平的时 ...
- [C]编译器对char数组声明的一个行为
1概述 如果使用char[]来声明char数组,那么编译器会自动计算后面的字面量字符数,再加上一个空字符,作为它的长度.实际上这个数组最后一位被编译器强行加上了\0: #include <std ...
- Codeforces Round #602 (Div. 2, based on Technocup 2020 Elimination Round 3
A,有多个线段,求一条最短的线段长度,能过覆盖到所又线段,例如(2,4)和(5,6) 那么我们需要4 5连起来,长度为1,例如(2,10)(3,11),用(3,10) 思路:我们想一下如果题目说的是最 ...