★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:https://www.cnblogs.com/strengthen/p/10128172.html 
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

目录:[Swift]通天遁地Swift

本文将演示动态标签的使用,它允许用户在标签上进行互动操作。

点击【Podfile】,查看安装配置文件。

1 platform :ios, '12.0'
2 use_frameworks!
3 
4 target 'DemoApp' do
5     source 'https://github.com/CocoaPods/Specs.git'
6     pod 'ActiveLabel'
7 end

根据配置文件的内容,进行动态标签的安装,

然后点击【DemoApp.xcworkspace】项目文件,打开已经安装动态标签的空白项目。

在项目导航区,打开视图控制器的代码文件【ViewController.swift】

现在编写代码,在项目中使用刚刚安装的动态标签。

  1 import UIKit
  2 //在类文件中引入动态标签
  3 import ActiveLabel
  4 
  5 class ViewController: UIViewController {
  6 
  7     override func viewDidLoad() {
  8         super.viewDidLoad()
  9         // Do any additional setup after loading the view, typically from a nib.
 10         //第一种动态标签。
 11         activeLabel()
 12         //第二种更强大的动态标签
 13         customizeLabel()
 14     }
 15     
 16     //添加一个方法,用来制作动态标签示例
 17     func activeLabel()
 18     {
 19         //创建一个原点在(0,0),宽度和高度都是320的标签。
 20         let label = ActiveLabel(frame: CGRect(x: 0, y: 0, width: 320, height: 320))
 21         
 22         //动态标签是对普通标签的扩展,所以同样拥有普通标签视图的各种属性。
 23         //在此设置标签视图不限制行数。
 24         label.numberOfLines = 0
 25         //然后设置动态标签的交互属性,
 26         //包含:主题标签、提及和网址等类型。
 27         //也就是说这些内容在标签视图中是可被点击的。
 28         label.enabledTypes = [.mention, .hashtag, .url]
 29         //设置动态标签的文字内容,
 30         //内容包含:一个主题标签 + 一个提交符号
 31         //两个符号和它们后面的文字内容将可被点击。
 32         label.text = "This is a post with #hashtags and a @userhandle."
 33         //设置文字的颜色为黑色
 34         label.textColor = .black
 35         //给主题标签添加交互事件,
 36         //当用户点击该内容时,在控制台输出日志信息,
 37         label.handleHashtagTap
 38         {
 39             hashtag in
 40             //日志信息包含被点击的标签内容。
 41             print("Success. You just tapped the \(hashtag) hashtag")
 42         }
 43         
 44         //将标签视图添加到当前视图控制器的根视图
 45         self.view.addSubview(label)
 46         //设置根视图的背景颜色为橙色
 47         self.view.backgroundColor = UIColor.orange
 48     }
 49 
 50     //创建更加强大的动态标签
 51     //添加一个方法,在这个新方法中,创建动态标签。
 52     func customizeLabel()
 53     {
 54         //初始化一个指定显示区域的动态标签
 55         let label = ActiveLabel(frame: CGRect(x: 20, y: 40, width: view.frame.width - 40, height: 300))
 56         //将该动态标签添加到当前视图控制器的根视图
 57         view.addSubview(label)
 58         
 59         //通过正则表达式创建自定义的动作类型
 60         let customType = ActiveType.custom(pattern: "\\sare\\b")
 61         let customType2 = ActiveType.custom(pattern: "\\ssupports\\b")
 62         
 63         //将自定义的动作类型,添加到动态标签所支持的类型列表中。
 64         label.enabledTypes.append(customType)
 65         label.enabledTypes.append(customType2)
 66         
 67         //设置网址文字的最大长度,当超出该长度的数值时,将截取网址并在尾部添加省略号。
 68         label.urlMaximumLength = 31
 69         
 70         //接着对动态标签的外观属性进行自定义设置
 71         label.customize
 72         {
 73             label in
 74             //设置动态标签的文字内容,文字内容中包含了各种动态类型
 75             label.text = "This is a post with #multiple #hashtags and a @userhandle. Links are also supported like" +
 76                 " this one: https://www.cnblogs.com/strengthen/. Now it also supports custom patterns -> are\n\n" +
 77             "Let's trim a long link: \nhttps://www.cnblogs.com/strengthen/p/10022619.html"
 78             //设置标签的行数
 79             label.numberOfLines = 0
 80             //设置标签的行间距
 81             label.lineSpacing = 4
 82             
 83             //设置动态标签对象的文字颜色
 84             label.textColor = UIColor(red: 102.0/255, green: 117.0/255, blue: 127.0/255, alpha: 1)
 85             //设置动态标签对象的主题标签文字的颜色
 86             label.hashtagColor = UIColor(red: 85.0/255, green: 172.0/255, blue: 238.0/255, alpha: 1)
 87             //设置动态标签的提及文字的颜色
 88             label.mentionColor = UIColor(red: 238.0/255, green: 85.0/255, blue: 96.0/255, alpha: 1)
 89             //设置动态标签的网址文字的颜色
 90             label.URLColor = UIColor(red: 85.0/255, green: 238.0/255, blue: 151.0/255, alpha: 1)
 91             //设置动态标签对象的网址被选中时的颜色
 92             label.URLSelectedColor = UIColor(red: 82.0/255, green: 190.0/255, blue: 41.0/255, alpha: 1)
 93             
 94             //设置当用户点击动态标签中的主题标签或提及文字时,将弹出提示框,显示相应内容
 95             label.handleMentionTap { self.alert("Mention", message: $0) }
 96             label.handleHashtagTap { self.alert("Hashtag", message: $0) }
 97             
 98             //添加网址的点击事件
 99             label.handleURLTap
100             {
101                 //获得网址对象
102                 let url = URL(string: $0.absoluteString)
103                 //调用应用程序对象,在浏览器中打开该网址
104                 UIApplication.shared.openURL(url!)
105             }
106             
107             //设置动态标签的第一种自定义动态类型的颜色
108             label.customColor[customType] = UIColor.purple
109             //和自定义类型被选中时的颜色
110             label.customSelectedColor[customType] = UIColor.green
111             //设置动态标签的第二种自定义动态类型的颜色
112             label.customColor[customType2] = UIColor.magenta
113             //和自定义类型被选中时的颜色
114             label.customSelectedColor[customType2] = UIColor.green
115             
116             //给两个自定义的动态类型添加点击事件,
117             //当用户点击时,将弹出提示框,显示相应的内容
118             label.handleCustomTap(for: customType) { self.alert("Custom type", message: $0) }
119             label.handleCustomTap(for: customType2) { self.alert("Custom type", message: $0) }
120         }
121     }
122     
123     //添加一个方法,用来响应点击事件
124     func alert(_ title: String, message: String)
125     {
126         //创建一个警告弹出窗口,并设置弹出窗口的标题、信息和样式等属性
127         let vc = UIAlertController(title: title, message: message, preferredStyle: UIAlertController.Style.alert)
128         //给弹出窗口添加一个按钮,当点击该按钮时,关闭弹出窗口
129         vc.addAction(UIAlertAction(title: "Ok", style: .cancel, handler: nil))
130         
131         //在当前的视图控制器中,以模态的方式弹出窗口。
132         present(vc, animated: true, completion: nil)
133     }
134     
135     override func didReceiveMemoryWarning() {
136         super.didReceiveMemoryWarning()
137         // Dispose of any resources that can be recreated.
138     }
139 }

 

转载于:https://www.cnblogs.com/strengthen/p/10128172.html

Logo

瓜分20万奖金 获得内推名额 丰厚实物奖励 易参与易上手

更多推荐