1. 程式人生 > >[Swift通天遁地]一、超級工具-(1)動態標籤:給UILabel文字中的Flag和url新增點選事件

[Swift通天遁地]一、超級工具-(1)動態標籤:給UILabel文字中的Flag和url新增點選事件

本文將演示動態標籤的使用,它允許使用者在標籤上進行互動操作。

點選【Podfile】,檢視安裝配置檔案。

1 platform :ios, '8.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 }