[Swift通天遁地]9、拔劍吧-(2)在項目中使用大量美觀的圖標

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:山青詠芝(shanqingyongzhi)
➤博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:http://www.javashuo.com/article/p-arjcjxxr-md.html 
➤若是連接不是山青詠芝的博客園地址,則多是爬取做者的文章。
➤原文已修改更新!強烈建議點擊原文地址閱讀!支持做者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html

目錄:[Swift]通天遁地Swiftios

本文將演示如何在項目中添加大量的字體類型的矢量圖標。git

首先確保已經安裝了所需的第三方類庫。雙擊查看安裝配置文件【Podfile】github

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

根據配置文件中的相關設置,安裝第三方類庫。swift

安裝完成以後,雙擊打開項目文件【DemoApp.xcodeproj】數組

在左側的項目導航區,打開視圖控制器的代碼文件【ViewController.swift】xcode

 1 import UIKit
 2 //引入已經安裝的第三方類庫
 3 import FontAwesome_swift
 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         //初始化一個指定顯示區域的標籤對象
12         let label = UILabel(frame: CGRect(x: 20, y: 40, width: 280, height: 40))
13         //設置標籤對象的字體屬性,在此使用第三方類庫的字體
14         label.font = UIFont.fontAwesome(ofSize: 20, style: .brands)
15         //經過調用字符串的類方法,得到facebookSquare字符圖標,並使用該標籤對象顯示該字符圖標。
16         label.text = String.fontAwesomeIcon(name: .facebookSquare)
17         //將標籤對象添加到根視圖
18         self.view.addSubview(label)
19         
20         //初始化另外一個標籤對象
21         let label2 = UILabel(frame: CGRect(x: 20, y: 100, width: 280, height: 40))
22         //設置標籤對象使用第三方類庫的字體
23         label2.font = UIFont.fontAwesome(ofSize: 20, style: .brands)
24         //經過調用字符串的類方法,得到指定名稱的字符圖標,並使用標籤對象顯示該字符圖標。
25         label2.text = "GitHub:\(String.fontAwesomeIcon(name: .github))"
26         //將第二個標籤對象添加到根視圖
27         self.view.addSubview(label2)
28         
29         //初始化一組字符圖標
30         let icons = [FontAwesome.font,FontAwesome.bold,FontAwesome.italic,FontAwesome.underline,FontAwesome.link,FontAwesome.table, FontAwesome.list]
31         //添加一個七次的循環語句,以建立七個按鈕控件,
32         //每一個按鈕控件,各自顯示數組中的一張圖標。
33         for i in 0...6
34         {
35             //初始化一個指定顯示區域的按鈕控件
36             let button = UIButton(frame: CGRect(x: 20+i*32, y: 160, width: 30, height: 30))
37             //設置按鈕標籤的字體屬性,在此使用第三方類庫的字體,
38             button.titleLabel?.font = UIFont.fontAwesome(ofSize: 14, style: .solid)
39             //設置按鈕在正常狀態下的標題文字,
40             //這裏該標題文字設置爲數組中的圖標。
41             button.setTitle(String.fontAwesomeIcon(name: icons[i]), for: .normal)
42             //設置按鈕的背景顏色
43             button.backgroundColor = UIColor.orange
44             //將按鈕添加到根視圖
45             self.view.addSubview(button)
46         }
47         
48         //除了標籤和按鈕以外,還能夠使用圖像視圖控件,顯示第三方類庫中的字符圖標。
49         let imageView = UIImageView(frame: CGRect(x: 10, y: 200, width: 100, height: 100))
50         //設置圖像視圖控件的圖片屬性,在此顯示一枚紫色的相機圖標,
51         //因爲字符圖標爲矢量樣式,因此能夠自由設置圖標的尺寸。
52         imageView.image = UIImage.fontAwesomeIcon(name: .cameraRetro, style: .solid, textColor: UIColor.purple, size: CGSize(width: 100, height: 100))
53         
54         //將圖像視圖添加到根視圖
55         self.view.addSubview(imageView)
56     }
57 
58     override func didReceiveMemoryWarning() {
59         super.didReceiveMemoryWarning()
60         // Dispose of any resources that can be recreated.
61     }
62 }
相關文章
相關標籤/搜索