当前位置:网站首页>Textkit custom uilabel identification link
Textkit custom uilabel identification link
2022-07-29 01:10:00 【Hanyang Li】
1. Inherit DemoLabel.swift file , Inherit UILabel
class DemoLabel: UILabel {
//MARK: - Overridden properties
override var text: String?{
didSet{
prepareText()
}
}
override var attributedText: NSAttributedString?{
didSet{
prepareText()
}
}
//MARK: - Draw text
override func draw(_ rect: CGRect) {
// Area
let range = NSRange(location: 0, length: textStorage.length)
// draw - Layout manager drawing textStorage The content in
layoutManager.drawGlyphs(forGlyphRange: range, at: CGPoint.zero)
}
//MARK: - Constructors
override init(frame: CGRect) {
super.init(frame: frame)
prepareTextSystem()
}
required init?(coder: NSCoder) {
super.init(coder: coder)
prepareTextSystem()
}
// Prepare the text system
private func prepareTextSystem(){
//1. Prepare text
prepareText()
//2. Set object relationships
textStorage.addLayoutManager(layoutManager)
layoutManager.addTextContainer(textContainer)
}
/// Prepare text content
private func prepareText(){
if attributedText != nil{
// Set the attribute text directly
textStorage.setAttributedString(attributedText!)
}else if text != nil{
textStorage.setAttributedString(NSAttributedString(string: text!))
}else {
textStorage.setAttributedString(NSAttributedString(string: ""))
}
//print(rangeForUrls)
// Traverse url Array , Set text properties
guard let rangeForUrls = rangeForUrls else{
return
}
for range in rangeForUrls{
textStorage.addAttribute(.foregroundColor, value: UIColor.blue, range: range)
}
}
/// Prepare text content
override func layoutSubviews() {
super.layoutSubviews()
// Set the size of the text container
textContainer.size = bounds.size
}
//MARK: - Lazy load properties
// Text content - Subclasses of variable attribute strings
private lazy var textStorage = NSTextStorage()
// Layout manager - Responsible for text drawing
private lazy var layoutManager = NSLayoutManager()
// Text container - Set the size of the drawing
private lazy var textContainer = NSTextContainer()
}
//MARK: - Regular expression correlation function / attribute
extension DemoLabel{
/// Returns all in the string URL Linked range array
var rangeForUrls: [NSRange]?{
//1. Define regular expressions
let pattern = "[a-zA-Z]*://[a-zA-Z0-9/\\.]*"
let regex = try! NSRegularExpression(pattern: pattern, options: [])
//2. Match all url link
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. stay Main.storyboard Add UILabel Control , To test
@IBOutlet weak var label: DemoLabel!
override func viewDidLoad() {
super.viewDidLoad()
label.text = "http://www.apple.com http://www.hh.cn"
}
边栏推荐
- [Commons lang3 topic] 001 stringutils topic
- 一元函数积分学之1__不定积分
- ThinkPHP高仿蓝奏云网盘系统程序
- 电子招标初学者指南
- Protective copy & stateless
- [Commons lang3 topic] 002 randomutils topic
- 图扑软件亮相 2022 福州数博会,携手共创数字新时代
- Summary of process and thread knowledge points 1
- 可视化全链路日志追踪
- The digitalization of the consumer industry is upgraded to "rigid demand", and weiit's new retail SaaS empowers enterprises!
猜你喜欢
![“index [hotel/jXLK5MTYTU-jO9WzJNob4w] already exists“](/img/f2/37a1e65eb1104d72128f96fc5d9c85.png)
“index [hotel/jXLK5MTYTU-jO9WzJNob4w] already exists“

Copy the table in word to wechat as a picture and send it

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

Educational Codeforces Round 132 (Rated for Div. 2)【A~C】

小程序毕设作品之微信校园浴室预约小程序毕业设计成品(7)中期检查报告

Wechat campus bathroom reservation applet graduation design finished product (5) assignment

Method of converting inline elements to block elements
![[raspberry pie] how does the windows computer connect with raspberry pie](/img/d6/42685bbc4e4af757867442b63ce9c8.png)
[raspberry pie] how does the windows computer connect with raspberry pie

Hilbert 变换与瞬时频率

Day2: 130 questions in three languages
随机推荐
B+ tree~
Talk about the cross end technical scheme
【刷题笔记】二进制链表转整数
可视化全链路日志追踪
y80.第四章 Prometheus大厂监控体系及实战 -- kube-state-metrics组件介绍和监控扩展(十一)
Univariate function integration 1__ Indefinite integral
TextKit 自定义UILabel识别链接
ThinkPHP高仿蓝奏云网盘系统程序
How to explain JS' bind simulation implementation to your girlfriend
dart数组,Map,类型判断,条件判断运算符,类型转换
FLV文件简介
Consumer unit 消费单元
🧐 Table1 | finish your third line watch in one second
小程序毕设作品之微信校园浴室预约小程序毕业设计成品(8)毕业设计论文模板
Day2: 130 questions in three languages
Summary of process and thread knowledge points 2
用CDO进行nc数据的不规则裁剪
【unity】将unity编辑c#配置为vscode
[target detection] Introduction to yolor theory + practical test visdrone data set
ActiveMQ基本详解