当前位置:网站首页>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"
}
边栏推荐
- Interview shock 69: is TCP reliable? Why?
- Plato launched the LAAS protocol elephant swap, which allows users to earn premium income
- y80.第四章 Prometheus大厂监控体系及实战 -- kube-state-metrics组件介绍和监控扩展(十一)
- 追踪伦敦银实时行情的方法有哪些?
- [target detection] Introduction to yolor theory + practical test visdrone data set
- AQS principle
- 【目标检测】YOLOR理论简介+实践测试VisDrone数据集
- 异步模式之工作线程
- Send SMS verification code asynchronously using Ronglian cloud celery
- Self made | a 16 bit RISC architecture CPU is self-made by hand
猜你喜欢

Tips for API interface optimization

Implement Lmax disruptor queue from scratch (VI) analysis of the principle of disruptor solving pseudo sharing and consumers' elegant stopping

面试突击69:TCP 可靠吗?为什么?

Error reporting: when the browser clicks the modify add button, there is no response and no error reporting. Solution

散列表 ~

小程序毕设作品之微信校园浴室预约小程序毕业设计成品(6)开题答辩PPT

如何执行建设项目的时间影响分析?

SystemVerilog-连接和复制运算符

DDD领域驱动设计如何进行工程化落地

Some considerations about ThreadPool
随机推荐
华为发布HarmonyOS 3.0,向“万物互联”再迈一步
Wechat campus bathroom reservation for the finished product of applet graduation design (7) mid term inspection report
小程序毕设作品之微信校园浴室预约小程序毕业设计成品(8)毕业设计论文模板
[Commons lang3 topic] 002 randomutils topic
DRF - paging, JWT introduction and principle, JWT quick use, JWT source code analysis, JWT custom return format, custom user issued token, custom token authentication class
[web development] basic knowledge of flask framework
【commons-lang3专题】004- NumberUtils 专题
【unity】将unity编辑c#配置为vscode
Time series prediction | MATLAB realizes time series prediction of TCN time convolution neural network
selenium对接代理与seleniumwire访问开发者工具NetWork
[Yugong series] go teaching course in July 2022, an array of 020 go containers
What opportunities does the London gold real-time market bring?
[raspberry pie] how does the windows computer connect with raspberry pie
B- 树 ~
Irregular clipping of NC data with CDO
Main thread and daemon thread
Copu Professor Lu Shouqun was invited to give a keynote speech at the open atom global open source summit
B+ 树 ~
进程和线程知识点总结 2
面试突击69:TCP 可靠吗?为什么?