当前位置:网站首页>TextKit 自定义UILabel识别链接
TextKit 自定义UILabel识别链接
2022-07-28 23:52:00 【Hanyang Li】
1. 继承 DemoLabel.swift 文件,继承 UILabel
class DemoLabel: UILabel {
//MARK: - 重写属性
override var text: String?{
didSet{
prepareText()
}
}
override var attributedText: NSAttributedString?{
didSet{
prepareText()
}
}
//MARK: - 绘制文本
override func draw(_ rect: CGRect) {
//区域
let range = NSRange(location: 0, length: textStorage.length)
//绘制 - 布局管理器绘制 textStorage 中的内容
layoutManager.drawGlyphs(forGlyphRange: range, at: CGPoint.zero)
}
//MARK: - 构造函数
override init(frame: CGRect) {
super.init(frame: frame)
prepareTextSystem()
}
required init?(coder: NSCoder) {
super.init(coder: coder)
prepareTextSystem()
}
//准备文本系统
private func prepareTextSystem(){
//1.准备文本
prepareText()
//2.设置对象关系
textStorage.addLayoutManager(layoutManager)
layoutManager.addTextContainer(textContainer)
}
///准备文本内容
private func prepareText(){
if attributedText != nil{
//直接设置属性文本
textStorage.setAttributedString(attributedText!)
}else if text != nil{
textStorage.setAttributedString(NSAttributedString(string: text!))
}else {
textStorage.setAttributedString(NSAttributedString(string: ""))
}
//print(rangeForUrls)
//遍历 url 数组,设置文本属性
guard let rangeForUrls = rangeForUrls else{
return
}
for range in rangeForUrls{
textStorage.addAttribute(.foregroundColor, value: UIColor.blue, range: range)
}
}
/// 准备文本内容
override func layoutSubviews() {
super.layoutSubviews()
//设置文本容器的尺寸
textContainer.size = bounds.size
}
//MARK: - 懒加载属性
//文本内容 - 可变属性字符串的子类
private lazy var textStorage = NSTextStorage()
//布局管理器-负责文本绘制
private lazy var layoutManager = NSLayoutManager()
//文本容器 - 设置绘制的尺寸
private lazy var textContainer = NSTextContainer()
}
//MARK: - 正则表达式相关函数/属性
extension DemoLabel{
///返回字符串中所有 URL 链接的范围数组
var rangeForUrls: [NSRange]?{
//1.定义正则表达式
let pattern = "[a-zA-Z]*://[a-zA-Z0-9/\\.]*"
let regex = try! NSRegularExpression(pattern: pattern, options: [])
//2.匹配所有的 url 链接
let result = regex.matches(in: textStorage.string, options: [], range: NSRange(location: 0, length: textStorage.length))
var array = [NSRange]()
for m in result {
array.append(m.range(at: 0))
}
return array
}
}
2. 在 Main.storyboard 中添加 UILabel 控件,进行测试
@IBOutlet weak var label: DemoLabel!
override func viewDidLoad() {
super.viewDidLoad()
label.text = "http://www.apple.com http://www.hh.cn"
}
边栏推荐
- Time series prediction | MATLAB realizes time series prediction of TCN time convolution neural network
- Error reporting: Rong Lianyun sends SMS verification code message 500
- Day2:三种语言暴刷牛客130题
- Copy the table in word to wechat as a picture and send it
- Jupyter notebook中5个有趣的魔法命令
- New pseudo personal guide page source code
- [Commons lang3 topic] 002 randomutils topic
- 新一代超安全蜂窝电池,思皓爱跑上市,13.99万起售
- Five interesting magic commands in jupyter notebook
- [raspberry pie] how does the windows computer connect with raspberry pie
猜你喜欢

关于ThreadPool的一些注意事项

【无标题】

New pseudo personal guide page source code
![Cloud function realizes website automatic check-in configuration details [web function /nodejs/cookie]](/img/e3/496247afdb3ea5b9a9cdb8afb0d41b.png)
Cloud function realizes website automatic check-in configuration details [web function /nodejs/cookie]

用CDO进行nc数据的不规则裁剪

状态压缩dp-蒙德里安的梦想

Huawei releases harmonyos 3.0, taking another step towards "Internet of all things"

如何处理项目中的时间、范围和成本限制?

Consumer unit

【目标检测】YOLOR理论简介+实践测试VisDrone数据集
随机推荐
Daniel guild Games: summary and future outlook of this year
[Yugong series] go teaching course in July 2022, an array of 020 go containers
【AD学习】本次海上航行器大赛画pcb图的历程
DDD领域驱动设计如何进行工程化落地
【刷题笔记】二进制链表转整数
Classification prediction | MATLAB realizes time series classification prediction of TCN time convolution neural network
Cloud function realizes website automatic check-in configuration details [web function /nodejs/cookie]
AQS原理
Selenium docking agent and selenium wire access developer tool network
day8
B-tree~
【无标题】
system verilog常用语法
day8
Main thread and daemon thread
双链表的定义 ~
Time series prediction | MATLAB realizes time series prediction of TCN time convolution neural network
用CDO进行nc数据的不规则裁剪
y80.第四章 Prometheus大厂监控体系及实战 -- kube-state-metrics组件介绍和监控扩展(十一)
可视化全链路日志追踪