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 ...
随机推荐
- Cesium 限制相机进入地下
有时我们在Cesium操作时,点击鼠标中间滚轮可更改视角,有时会使相机进入地下,导致体验很差,网上说了很多中方法,效果都不好或者没效果,下面是我翻了源码找到的方法,亲测有效.如有问题可按照专栏上的联系 ...
- 'OracleInternal.MTS.DTCPSPEManager' 类型初始值设定项引发异常
环境:VS2010,.NET Framework 4.0,Oracle.ManagedDataAccess 在最近做一个项目中,用到了Oracle数据库,使用Oracle.ManagedData ...
- Nginx:基本概念
守住一方平安,尽力而为,问心无愧就好. Nginx同Apache一样都是一种WEB服务器,Nginx是一款自由的.开源的.高性能的HTTP服务器和反向代理服务器:同时也是一个IMAP.POP3 ...
- Redis学习(二)Redis的安装
Window 下安装 下载地址:https://github.com/MSOpenTech/redis/releases. Redis 支持 32 位和 64 位.这个需要根据你系统平台的实际情况选择 ...
- 修改Android源码实现原生应用双开,应用多开
1. 准备 把某系统双开的两个app的信息进行对比 1.1 目录的对比 1.1.1 data目录对比 原应用: /data/user/0/com.luoyesiqiu.crackme/files 被复 ...
- glusterFS空间不够了怎么办
查看glusterFS情况 oc project infra-storage oc get all #找到其中一个pod,前缀为 po/glusterfs-registry-xxxx oc exec ...
- IDEA去除掉虚线,波浪线,和下划线实线的方法
初次安装使用IDEA,总是能看到导入代码后,出现很多的波浪线,下划线和虚线,这是IDEA给我们的一些提示和警告,但是有时候我们并不需要,反而会让人看着很不爽,这里简单记录一下自己的调整方法,供其他的小 ...
- ubuntu 安装在硬盘与配置
安装 下载Ubuntu ISO文件,使用rufus制作启动U盘,重启选择这个U盘启动. 用rufus做启动盘时,提示缺少文件,点下载,找到log,进入找到下载地址,手动下载,并放到软件所在路径下的文件 ...
- python 装饰器使用总结
python 装饰器使用总结 by:授客 QQ:1033553122 测试环境 win10 python 3.5 例1:一个简单的例子 #!/usr/bin/env python # -*- codi ...
- java使用htmlunit工具抓取js中加载的数据
htmlunit 是一款开源的java 页面分析工具,读取页面后,可以有效的使用htmlunit分析页面上的内容.项目可以模拟浏览器运行,被誉为java浏览器的开源实现.这个没有界面的浏览器,运行速度 ...