当前位置:网站首页>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"
}
边栏推荐
猜你喜欢

浅谈一下跨端技术方案

小程序毕设作品之微信校园浴室预约小程序毕业设计成品(5)任务书
![“index [hotel/jXLK5MTYTU-jO9WzJNob4w] already exists“](/img/f2/37a1e65eb1104d72128f96fc5d9c85.png)
“index [hotel/jXLK5MTYTU-jO9WzJNob4w] already exists“

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

ThinkPHP高仿蓝奏云网盘系统程序

Wechat campus bathroom reservation applet graduation design finished product (8) graduation design thesis template

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

PLATO上线LAAS协议Elephant Swap,用户可借此获得溢价收益

Time series prediction | MATLAB realizes time series prediction of TCN time convolution neural network

面试突击69:TCP 可靠吗?为什么?
随机推荐
FLV文件简介
Interview shock 69: is TCP reliable? Why?
What opportunities does the London gold real-time market bring?
【刷题笔记】二进制链表转整数
Seven marketing strategies of NFT project
自制 | 纯手工自制一个16位RISC架构CPU
How to deal with the time, scope and cost constraints in the project?
Wechat campus bathroom reservation applet graduation design finished product (8) graduation design thesis template
RHCE命令练习(二)
Visual full link log tracking
A new generation of ultra safe cellular battery, Sihao aipao, is on the market, starting from 139900
system verilog常用语法
Definition of double linked list~
教你一文解决 js 数字精度丢失问题
Hilbert 变换与瞬时频率
Time series prediction | MATLAB realizes time series prediction of TCN time convolution neural network
Implement Lmax disruptor queue from scratch (VI) analysis of the principle of disruptor solving pseudo sharing and consumers' elegant stopping
Instruction rearrangement, happens before, as if serial
分类预测 | MATLAB实现TCN时间卷积神经网络的时序分类预测
Summary of process and thread knowledge points 1