当前位置:网站首页>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"
}
边栏推荐
- Cookie和Session
- ThinkPHP高仿蓝奏云网盘系统程序
- Mathematical modeling and detailed explanation of basic knowledge (common knowledge points of Chemistry)
- DRF - web development mode, API interface, API interface testing tool, restful specification, serialization and deserialization, DRF installation and use
- Day2:三种语言暴刷牛客130题
- Error reporting: Rong Lianyun sends SMS verification code message 500
- 返回*this的成员函数
- [notes for question brushing] specified interval reversal in the linked list
- ThinkPHP high imitation blue cloud disk system program
- PLATO上线LAAS协议Elephant Swap,用户可借此获得溢价收益
猜你喜欢

Huawei releases harmonyos 3.0, taking another step towards "Internet of all things"
![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]

Daniel guild Games: summary and future outlook of this year

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

Charles -- 从0-1教你如何使用抓包工具

如何在WordPress中创建一个自定义404错误页面

Tips for API interface optimization

Charles -- teach you how to use the packet capturing tool from 0-1

DRF - web development mode, API interface, API interface testing tool, restful specification, serialization and deserialization, DRF installation and use

Deep learning | matlab implementation of TCN time convolution neural network spatialdropoutlayer parameter description
随机推荐
ThinkPHP high imitation blue cloud disk system program
Educational Codeforces Round 132 (Rated for Div. 2)【A~C】
Method of converting inline elements to block elements
SystemVerilog-连接和复制运算符
DRF - web development mode, API interface, API interface testing tool, restful specification, serialization and deserialization, DRF installation and use
Implement Lmax disruptor queue from scratch (VI) analysis of the principle of disruptor solving pseudo sharing and consumers' elegant stopping
Copu Professor Lu Shouqun was invited to give a keynote speech at the open atom global open source summit
【刷题笔记】二进制链表转整数
Mathematical modeling and detailed explanation of basic knowledge (common knowledge points of Chemistry)
【AD学习】本次海上航行器大赛画pcb图的历程
消费行业数字化升级成“刚需”,weiit新零售SaaS为企业赋能!
NFT 项目的 7 种市场营销策略
[Commons lang3 topic] 003- randomstringutils topic
小程序毕设作品之微信校园浴室预约小程序毕业设计成品(5)任务书
DRF -- authentication, authority, frequency source code analysis, global exception handling, automatic generation of interface documents, RBAC introduction
ACM SIGIR 2022 | 美团技术团队精选论文解读
散列表 ~
管理区解耦架构见过吗?能帮客户搞定大难题的
ActiveMQ基本详解
Educational Codeforces Round 132 (Rated for Div. 2)【A~C】