[Swift通天遁地]九、拔剑吧-(2)在项目中使用大量美观的图标

本文将演示如何在项目中添加大量的字体类型的矢量图标。

首先确保已经安装了所需的第三方类库。双击查看安装配置文件【Podfile】

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

根据配置文件中的相关设置,安装第三方类库。

安装完成之后,双击打开项目文件【DemoApp.xcodeproj】

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

 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 }

猜你喜欢

转载自www.cnblogs.com/strengthen/p/10355486.html