14、iOS里面的富文本

2023-05-16

iOS里面的富文本

  • 1、NSAttributedString属性概览表
  • 2、属性详解及应用
    • 2.1 NSAttributedString.Key.font --字体大小
    • 2.2 NSAttributedString.Key.paragraphStyle -- 文本字、行间距,对齐等
    • 2.3 NSAttributedString.Key.foregroundColor -- 字体颜色
    • 2.4 NSAttributedString.Key.backgroundColor -- 背景色
    • 2.5 NSAttributedString.Key.ligature -- 连字符
    • 2.6 NSAttributedString.Key.kern -- 字符间距
    • 2.7 设置删除线相关
    • 2.8 设置下划线相关
    • 2.9 描边颜色、描边宽度
    • 2.10 文本阴影
    • 2.11 文字效果
    • 2.12 链接
    • 2.13 基础偏移量
    • 2.14 字体倾斜
    • 2.15 文本扁平化(横向拉伸)
  • 3、富文本加载html
  • 4、图文混排
    • 4.1 光标位置插入图片
    • 4.2 光标位置插入文字
  • 5、巧用UITextView实现富文本的点击效果
  • 参考文章

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

1、NSAttributedString属性概览表

KeyValue说明
.fontUIFont对象字体大小:默认Helvetica(Neue) 12
.paragraphStyleNSParagraphStyle对象文本字、行间距,对齐等:默认NSParagraphStyle.default
foregroundColorUIColor对象字体颜色:默认.black
.backgroundColorUIColor对象背景色:默认nil(无背景色)
.ligature包含整数的NSNumber对象连字符:ios中有0和1两个值;0表示没有连字符,而1是默认的连字符
.kern包含浮点数的NSNumber对象字符间距:默认0(禁用)
.strikethroughStyle包含整数的NSNumber对象删除线:默认0(无删除线)
.underlineStyle包含整数的NSNumber对象下划线:默认0(无下划线)
.strikethroughColorUIColor对象删除线颜色:默认 nil(和文字的 foregroundColor一致)
.underlineColorUIColor对象下划线颜色:默认nil(和文字的 foregroundColor一致)
.strokeColorUIColor对象描边颜色:nil(和文字的 foregroundColor一致)
strokeWidth包含浮点数的NSNumber对象描边宽度:正值空心描边,负值实心描边,默认0(不描边)
.shadowNSShadow对象文本阴影:默认nil(没有阴影)
.textEffectNSString对象文字效果:默认nil(没有文字效果)
.attachmentNSTextAttachment对象附件(常用作图文混排) :默认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(string: text)
        //2、添加属性
        attributeString.addAttribute(.kern, value: 10, range: NSMakeRange(text.count-title.count, title.count))
        //3、赋值
        label.attributedText = attributeString

在这里插入图片描述

2.7 设置删除线相关

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

在这里插入图片描述

2.8 设置下划线相关

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

在这里插入图片描述

2.9 描边颜色、描边宽度

  • 描边颜色要搭配非0的描边宽度才会生效,如果只设置了描边颜色,描边宽度为0,则没有描边效果
  • 描边宽度是正数,会对文字进行描边,但文字中心不填充( 一种经典的空心文本样式是在该值为3.0)
  • 描边宽度是负数,会对文字进行描边,而且会同时对文字中心进行填充(填充的颜色为文字本来的字体颜色)
        let title = "联系客服"
        let text = "遇到问题?您可以\(title)"
        //1、创建NSMutableAttributeString实例
        let attributeString = NSMutableAttributedString(string: text)
        
        //2、添加属性
        attributeString.addAttribute(.strokeWidth, value: 3, range: NSMakeRange(text.count-title.count, title.count))
        attributeString.addAttribute(.strokeColor, value: UIColor.black, range: NSMakeRange(text.count-title.count, title.count))
        //3、赋值
        label.attributedText = attributeString

2.10 文本阴影

        let title = "联系客服"
        let text = "遇到问题?您可以\(title)"
        //1、创建NSMutableAttributeString实例
        let attributeString = NSMutableAttributedString(string: text)
        
        //2、添加属性
        let shadow = NSShadow()
        shadow.shadowColor = UIColor.black
        shadow.shadowBlurRadius = 3
        shadow.shadowOffset = CGSize(width: 0, height: 0.8)
        attributeString.addAttribute(.shadow, value: shadow, range: NSMakeRange(text.count-title.count, title.count))
        //3、赋值
        label.attributedText = attributeString

在这里插入图片描述

2.11 文字效果

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

在这里插入图片描述

在这里插入图片描述

2.12 链接

UILabel无法使用该属性, 但UITextView 控件可以使用。

     let title = "联系客服"
        let text = "遇到问题?您可以\(title)"
        //1、创建NSMutableAttributeString实例
        let attributeString = NSMutableAttributedString(string: text)
        
        //2、添加属性
        let url = URL(string: "http://www.baidu.com")!
        attributeString.addAttribute(.link, value: url, range: NSMakeRange(text.count-title.count, title.count))
        //3、赋值
        textView.attributedText = attributeString
        //设置link的样式
        textView.linkTextAttributes = [.foregroundColor:UIColor.orange]

在这里插入图片描述

2.13 基础偏移量

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

在这里插入图片描述

2.14 字体倾斜

注意:正值向右倾斜,负值向左倾斜, 默认0(不倾斜)

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

在这里插入图片描述

2.15 文本扁平化(横向拉伸)

注意:正值横向拉伸,负值横向压缩,默认0(不拉伸)

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

在这里插入图片描述

3、富文本加载html

    let str = "<div>哈哈哈哈哈</div>"
        do {
            let attributeStr = try NSMutableAttributedString(data: str.data(using: .unicode)!, options: [NSAttributedString.DocumentReadingOptionKey.documentType : NSAttributedString.DocumentType.html], documentAttributes: nil)
            textView.attributedText = attributeStr
        }catch {
        }

在这里插入图片描述

4、图文混排

现在方案:
1、HTML结合Webview
2、利用CoreText,手动解析手动布局
3、第三方库,如YYText。
4、NSMutableAttriteString

  • 第一种方案,优点是:对于客户端来说,开发难度和代码量都是比较小的。同时也能做到随心所欲的布局,不需要考虑图片,缓存,和交互等等问题。缺点是:需要准备相应的Html页面,如果页面中涉及到权限验证,处理起来比较麻烦,另外,如果在业务场景复杂的情况下,针对性能和交互的优化需要花更多的时间。
  • 第二种方案,通过原生的CoreText,使我们能接管整个图文排版过程中数据的管理以及界面的排布展示,优点是:自由度高,能够随心所欲展示排版样式、交互方式,效率上比较高。缺点是:代码量比较大,需要自己造好几个轮子,并且因为需要关注的地方比较多,如:图片缓存,排版交互等,开发周期上会比较长,同时前后端数据交互格式也需要相互配合。
  • 第三种方案,这个方案省去造轮子的时间,可以将注意力放在排版和优化上,笔者采用的也是这个方案。
  • 第四种方案,只适合简单的图文混排。

4.1 光标位置插入图片

  enum ImageAttachmentMode {
        case `default` //默认(不改变大小)
        case fitTextLine //使得尺寸适应行高
        case fitTextView //使得尺寸适应textView
  }
 //光标位置插入图片
    func insertPic(_ textView:UITextView,_ image:UIImage,mode:ImageAttachmentMode = .default){
        //获取textView的所有文本,转化成可变的文本
        let mutableStr = NSMutableAttributedString(attributedString: textView.attributedText)
        
        //创建图片附件
        let imgAttachment = NSTextAttachment(data: nil, ofType: nil)
        imgAttachment.image = image
        
        //设置图片显示方式
        if mode == .fitTextLine {
            //与文字一样大小
            imgAttachment.bounds = CGRect(x: 0, y: -4, width: textView.font!.lineHeight, height: textView.font!.lineHeight)
        }else if mode == .fitTextView {
            //撑满一行
            let imageWidth = textView.frame.width - 10
            let imageHeight = image.size.height / image.size.width * imageWidth
            imgAttachment.bounds = CGRect(x: 0, y: 0, width: imageWidth, height: imageHeight)
        }
        
        let imgAttachmentStr =  NSAttributedString(attachment: imgAttachment)
        
        //获得目前光标的位置
        let selectedRange = textView.selectedRange
        //插入文字
        mutableStr.insert(imgAttachmentStr, at: selectedRange.location)
        //设置可变文字的字体属性
        mutableStr.addAttribute(.font, value: UIFont.systemFont(ofSize: 30), range: NSMakeRange(0, mutableStr.length))
        //再次记住新的光标的位置
        let newSelectedRange = NSMakeRange(selectedRange.location + 1, 0)
        
        //重新给文本赋值
        textView.attributedText = mutableStr
        //恢复光标的位置(上面一句代码执行之后,光标会移动最后面)
        textView.selectedRange = newSelectedRange
        //移动滚动条(确保光标在可视范围内)
        textView.scrollRangeToVisible(newSelectedRange)
    }

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

4.2 光标位置插入文字

 //光标位置插入文字
    func insertStr(_ textView:UITextView,_ text:String){
        //获取textView的所有文本,转变成可变的文本
        let mutableStr = NSMutableAttributedString(attributedString: textView.attributedText)
        //获取目前光标的位置
        let selectedRange = textView.selectedRange
        //插入文字
        let attrStr = NSAttributedString(string: text)
        mutableStr.insert(attrStr, at: selectedRange.location)
        
        //设置可变文本的字体属性
        mutableStr.addAttribute(.font, value: UIFont.systemFont(ofSize: 30), range: NSMakeRange(0, mutableStr.length))
        //再次记住新的光标的位置
        let newSelectedRange = NSMakeRange(selectedRange.location + attrStr.length, 0)
        
        //重新给文本赋值
        textView.attributedText = mutableStr
        //恢复光标的位置(上面一句代码执行之后,光标会移动到最后面)
        textView.selectedRange = newSelectedRange
        
    }

5、巧用UITextView实现富文本的点击效果

class RegisterController: BaseViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        //用UItextView 实现富文本的功能
        let textView = UITextView(frame: CGRect(x: 50, y: 200, width: 200, height: 100))
        textView.isEditable = false
        textView.isScrollEnabled = false
        textView.delegate = self
        view.addSubview(textView)
        
        let title = "联系客服"
        let text = "遇到问题?您可以\(title)"
        let attributeString = NSMutableAttributedString(string: text)
        attributeString.addAttribute(.font, value: UIFont.systemFont(ofSize: 15), range: NSMakeRange(0, text.count))
        attributeString.addAttribute(.link, value: "contact://", range: NSMakeRange(text.count-title.count, title.count))
        textView.attributedText = attributeString
        textView.linkTextAttributes = [.foregroundColor : UIColor.orange] //设置要点击内容的样式,否则会有默认样式
    }
}

extension RegisterController : UITextViewDelegate {
    func textView(_ textView: UITextView, shouldInteractWith URL: URL, in characterRange: NSRange, interaction: UITextItemInteraction) -> Bool {
        if URL.absoluteString.contains("contact://") {
            //处理联系客服的逻辑
            
            return false
        }
        return true
    }
}

在这里插入图片描述

参考文章

https://www.jianshu.com/p/1056d983bdfd

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

14、iOS里面的富文本 的相关文章

随机推荐

  • 2014年年终总结:写书成长,承载收获

    雪花纷飞 xff0c 任你飘落凝成魅力的雪域之城 美丽的守候 xff0c 望长城内外惟余莽莽 数着北国春夏秋冬的每一天 xff0c 2014 的日历天天换新装 xff0c 消瘦了你的时光 但丰盈了我的渴望 2014 年 xff0c 在你的身
  • Protobuf在Android中的基本使用

    前言 Protobuf xff0c 类似于json和xml xff0c 是一种序列化结构数据机制 xff0c 可以用于数据通讯等场景 xff0c 相对于xml而言更小 xff0c 相对于json而言解析更快 xff0c 支持多语言 一 Pr
  • CentOS 使用virsh创建虚拟机

    一 参考 xff1a 1 CentOS 7 6使用virsh创建虚拟机 2 先输入 xff1a yum search qemu kvm 查看是否有qemu kvm可以安装 接着输入 xff1a yum search libvirt 查看是否
  • linux离线搭建SVN服务器系列<二 >

    在linux离线搭建SVN服务器系列 lt 一 gt 里安装svn服务器后 xff0c 接下来就是安装svn客户端了 这里打算以windows客户端和linux为例进行说明 一 linux svn客户端 一 先安装linux svn客户端
  • NDK配置debug环境时:Error:FAILURE: Build failed with an exception

    Error FAILURE Build failed with an exception What went wrong Execution failed for task 39 app externalNativeBuildDebug 3
  • Android 集成OpenCV

    OpenCV下载 一 集成SDK 1 从官网下载最新的android sdk xff0c 这里下载的是4 5 4 2 创建Android项目 xff0c 将sdk以library方式引入项目中 sdk使用了kotlin xff0c 需要配置
  • MyEclipse 控制台等显示在底部

    虽然接触MyEclipse已经有了几年了 xff0c 但是底部的控制台 xff0c 服务 xff0c svn等等显示不清楚 今天百度了 xff0c 发现只要按 Window gt show view gt 选择你要固定在底部的选项 还真方便
  • 协方差与自相关

    协方差矩阵是一个矩阵 xff0c 其每个元素是各个向量元素之间的协方差 这是从标量随机变量 到高维度随机向量 的自然推广 假设是以个标量随机变量组成的列向量 xff0c 并且是其第i个元素的期望值 xff0c 即 协方差矩阵被定义的第i x
  • 基础解系

    基础解系首先是线性无关的 xff0c 简单的理解就是能够用它的线性组合表示出该 方程组的任意一组解 xff0c 基础解系是针对有无数多组解的方程而言 xff0c 若是齐次线性方程组则应是有效方程组的个数少于未知数的个数 xff0c 若非齐次
  • 机器学习实践指南:案例应用解析(第二版)

    试读及购买链接 机器学习实践指南2版代码及资源 原书中的360网盘链接因为360关闭网盘的原因已经失效 1 https pan baidu com s 1nw37A5N 2 http www hzbook com Books 9324 ht
  • 数学之路-python计算实战(7)-机器视觉-图像产生加性零均值高斯噪声

    图像产生加性零均值高斯噪声 xff0c 在灰度图上加上噪声 xff0c 加上噪声的方式是每个点的灰度值加上一个噪声值 xff0c 噪声值的产生方式为Box Muller算法 生成高斯噪声 在计算机模拟中 xff0c 经常需要生成正态分布的数
  • 数学之路-python计算实战(16)-机器视觉-滤波去噪(邻域平均法滤波)

    coding utf 8 code myhaspl 64 myhaspl com 邻域平均法滤波 半径为2 import cv2 import numpy as np fn 61 34 test3 jpg 34 myimg 61 cv2 i
  • Serializable和Parcelable序列化

    前言 Android中常用的序列化方式包含有两种 xff1a Serializable和Parcelable 其中Serializable是java中通用的对象序列化方法 xff0c 在Android实际内存操作时会更加偏向于实现Parce
  • R语言与数据模型(1)-平均,方差,中位数,分位数,极差

    1 求平均数 gt x lt c 1 10 20 30 40 50 NA 60 gt xm lt mean x gt xm 1 NA na rm表示允许缺失数据NA gt xm lt mean x na rm 61 TRUE gt xm 1
  • AI理论随笔-对称矩阵、正交矩阵与特征向量,特征值(2)

    一 如果 xff1a A A T 61 E AA T 61 E A A T 61 E
  • 英文过滤停用词

    span class token triple quoted string string 34 34 34 Created on Sun Nov 13 09 14 13 2016 64 author daxiong 34 34 34 spa
  • C语言随笔-去掉仅有\n的行

    include lt stdio h gt int main int argc const char argv char str 128 char linep strcpy str 34 12 35 56 n12 33 87 n n n n
  • python3.6-深入浅出视频

    课程收益 适合人群 python小白 xff0c 大数据和机器学习编程程序员 上机实践为主线 以最快的速度上手 快速入门 xff0c 还学到了python3的核心知识 https edu csdn net course detail 989
  • 数学之路(3)-机器学习(3)-机器学习算法-神经网络[11]

    多层感知器的代码 xff0c 需要一个比较复杂的调试过程 xff0c 不过也有一些方法来加快这一速度 xff0c 其中有几个地方要注意 xff1a 1 输入层 输出层 中间层的学习率和动量参数不能一样 xff0c 2 3个层的权值策略不能一
  • 14、iOS里面的富文本

    iOS里面的富文本 1 NSAttributedString属性概览表2 属性详解及应用2 1 NSAttributedString Key font 字体大小2 2 NSAttributedString Key paragraphStyl