14、iOS里面的富文本

ios项目中经常需要显示一些带有特殊样式的文本,比如说带有下划线、删除线、斜体、空心字体、背景色、阴影以及图文混排(一种文字中夹杂图片的显示效果)。通常想要实现这些效果要使用到iOS的Fundation框架提供的NSAttributeString类。

1、NSAttributedString属性概览表

Key Value 说明
.font UIFont对象 字体大小:默认Helvetica(Neue) 12
.paragraphStyle NSParagraphStyle对象 文本字、行间距,对齐等:默认NSParagraphStyle.default
foregroundColor UIColor对象 字体颜色:默认.black
.backgroundColor UIColor对象 背景色:默认nil(无背景色)
.ligature 包含整数的NSNumber对象 连字符:ios中有0和1两个值;0表示没有连字符,而1是默认的连字符
.kern 包含浮点数的NSNumber对象 字符间距:默认0(禁用)
.strikethroughStyle 包含整数的NSNumber对象 删除线:默认0(无删除线)
.underlineStyle 包含整数的NSNumber对象 下划线:默认0(无下划线)
.strikethroughColor UIColor对象 删除线颜色:默认 nil(和文字的 foregroundColor一致)
.underlineColor UIColor对象 下划线颜色:默认nil(和文字的 foregroundColor一致)
.strokeColor UIColor对象 描边颜色:nil(和文字的 foregroundColor一致)
strokeWidth 包含浮点数的NSNumber对象 描边宽度:正值空心描边,负值实心描边,默认0(不描边)
.shadow NSShadow对象 文本阴影:默认nil(没有阴影)
.textEffect NSString对象 文字效果:默认nil(没有文字效果)
.attachment NSTextAttachment对象 附件(常用作图文混排) :默认nil(没有附件)
.link(原名:NSLinkAttributeName) NSURL (优先) 或 NSString对象 链接
.baselineOffset 包含浮点数的NSNumber对象 基础偏移量:正值向上偏移,负值向下偏移,默认0(不偏移)
.obliqueness 包含浮点数的NSNumber对象 字体倾斜 :正值向右倾斜,负值向左倾斜, 默认0(不倾斜)
.expansion 包含浮点数的NSNumber对象 文本扁平化:正值横向拉伸,负值横向压缩,默认0(不拉伸)

2、属性详解及应用

NSMutableAttributedString 是 NSAttributedString 的子类,一般来说我比较习惯使用NSMutableAttributedString来实现富文本。

2.1 NSAttributedString.Key.font --字体大小

 let title = "联系客服"
        let text = "遇到问题?您可以\(title)"
        //1、创建NSMutableAttributeString实例
        let attributeString = NSMutableAttributedString(string: text)
        //2、添加属性
        attributeString.addAttribute(.font, value: UIFont.systemFont(ofSize: 30), range: NSMakeRange(text.count - title.count, title.count))
        //3、赋值
        label.attributedText = attributeString

在这里插入图片描述

2.2 NSAttributedString.Key.paragraphStyle – 文本字、行间距,对齐等

    let title = "联系客服"
        let text = "遇到问题?您可以\(title)"
        //1、创建NSMutableAttributeString实例
        let attributeString = NSMutableAttributedString(string: text)
        //2、添加属性
        let paragraphStyle = NSMutableParagraphStyle()
        paragraphStyle.alignment = .center  //居中对齐
        attributeString.addAttribute(.paragraphStyle, value: paragraphStyle, range: NSMakeRange(0, text.count))
        //3、赋值
        label.attributedText = attributeString

2.3 NSAttributedString.Key.foregroundColor – 字体颜色

   let title = "联系客服"
        let text = "遇到问题?您可以\(title)"
        //1、创建NSMutableAttributeString实例
        let attributeString = NSMutableAttributedString(string: text)
        //2、添加属性
        attributeString.addAttribute(.foregroundColor, value: UIColor.orange, range: NSMakeRange(text.count-title.count, title.count))
        //3、赋值
        label.attributedText = attributeString

在这里插入图片描述

2.4 NSAttributedString.Key.backgroundColor – 背景色

        let title = "联系客服"
        let text = "遇到问题?您可以\(title)"
        //1、创建NSMutableAttributeString实例
        let attributeString = NSMutableAttributedString(string: text)
        //2、添加属性
        attributeString.addAttribute(.backgroundColor, value: UIColor.orange, range: NSMakeRange(text.count-title.count, title.count))
        //3、赋值
        label.attributedText = attributeString

在这里插入图片描述

2.5 NSAttributedString.Key.ligature – 连字符

ios 中有 0 和 1 两个值:0表示没有连字符,而1是默认的连字符。(一般对连笔写的英文有效, 中文即使设置了连字符也很难表现出来)。

2.6 NSAttributedString.Key.kern – 字符间距

正值间距加宽,负值间距变窄,0表示默认效果

        let title = "联系客服"
        let text = "遇到问题?您可以\(title)"
        //1、创建NSMutableAttributeString实例
        let attributeString = NSMutableAttributedString(str
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值