周末闲着无聊,想起最近遇到一个关于文字显示设置的小问题,想着以后可能还会经常用到,所以趁这个时候一起整理一下,方便以后备用
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let text = "我是测试文本,不信你看我的显示效果是不是跟你设置的一样呢,如果不是的话你来追我啊,如果追到我我就给你,哈哈哈😄!"
let attributeText = NSMutableAttributedString.init(string: text)
let count = text.characters.count
//设置段落属性
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.lineSpacing = 20 //设置行间距
// paragraphStyle.firstLineHeadIndent = 40 //首行缩进距离
// paragraphStyle.headIndent = 50 //文本每一行的缩进距离
// paragraphStyle.tailIndent = 20 //文本行末缩进距离
paragraphStyle.alignment = .justified //文本对齐方向
// paragraphStyle.baseWritingDirection = .rightToLeft //文本排序方向
//关于设置段落的更多属性可以查看官方文档: https://developer.apple.com/reference/uikit/nsmutableparagraphstyle
attributeText.addAttributes([NSParagraphStyleAttributeName: paragraphStyle], range: NSMakeRange(0, count))
//设置部分粗体
attributeText.addAttributes([NSFontAttributeName: UIFont.boldSystemFont(ofSize: 16)], range: NSMakeRange(7, 18))
//设置文本背景颜色
// attributeText.addAttributes([NSBackgroundColorAttributeName: UIColor.yellow], range: NSMakeRange(0, count))
//设置前景颜色
attributeText.addAttributes([NSForegroundColorAttributeName: UIColor.green], range: NSMakeRange(7, 18))
//设置字距
attributeText.addAttributes([NSKernAttributeName: 5], range: NSMakeRange(0, count))
//设置下划线样式
attributeText.addAttributes([NSUnderlineStyleAttributeName: 1], range: NSMakeRange(0, count))
//设置下划线颜色
attributeText.addAttributes([NSUnderlineColorAttributeName: UIColor.red], range: NSMakeRange(0, count))
//设置阴影
let shadow = NSShadow()
shadow.shadowOffset = CGSize(width: 2, height: 2) //阴影偏移量
shadow.shadowColor = UIColor.init(red: 0.3, green: 0.3, blue: 0.3, alpha: 0.3) //阴影颜色
attributeText.addAttributes([NSShadowAttributeName: shadow], range: NSMakeRange(0, count))
//设置文字特殊效果
// attributeText.addAttributes([NSTextEffectAttributeName: NSTextEffectLetterpressStyle], range: NSMakeRange(0, count))
//设置字体倾斜
attributeText.addAttributes([NSObliquenessAttributeName: 0.7], range: NSMakeRange(0, count))
//设置字体扁平化
// attributeText.addAttributes([NSExpansionAttributeName: 0.5], range: NSMakeRange(0, count))
//设置方向: 这里不清楚属性值要传什么,所以设置之后没有效果,有知道的朋友希望能过留言告知一下
// attributeText.addAttributes([NSWritingDirectionAttributeName: NSWritingDirection.rightToLeft], range: NSMakeRange(0, 10))
//设置删除线
attributeText.addAttributes([NSStrikethroughStyleAttributeName: 1], range: NSMakeRange(0, count))
//设置删除线颜色
attributeText.addAttributes([NSStrikethroughColorAttributeName: UIColor.yellow], range: NSMakeRange(0, count))
let label = UILabel()
label.frame = CGRect(x: 40, y: 100, width: UIScreen.main.bounds.width - 80, height: 20)
label.numberOfLines = 0
label.attributedText = attributeText
label.sizeToFit()
self.view.addSubview(label)
}
//这里由于不知道在swift3.0之后如何把string.index转位Int,所以只好用比较死板的方法来进行设置
///设置文本部分粗体显示
private func set(string str: String, start str1: String, end str2: String, boldSize size: CGFloat) -> (NSMutableAttributedString) {
let nStr = NSMutableAttributedString.init(string: str)
//计算range
let array = str.characters
var i = 0
for a in array {
if String(a) == str1 {
print(i)
break
}
i += 1
}
var k = 0
for b in array {
if String(b) == str2 {
print(k)
break
}
k += 1
}
nStr.addAttributes([NSFontAttributeName: UIFont.boldSystemFont(ofSize: 18)], range: NSMakeRange(i, k))
return nStr
}
}
- 运行结果
网友评论