-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathFontAwesomeKit.swift
160 lines (128 loc) · 6.44 KB
/
FontAwesomeKit.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
//
// FontAwesomeKit.swift
//
// Created by Igor Gastov on 10/31/16.
// Copyright © 2016 Syndicode. All rights reserved.
//
import Foundation
import UIKit
// ---------- FontAwesomeKit.swift -------------
private let nameFont = "FontAwesome"
extension UILabel {
/// UILabel extension, which allows to set the icon from fontawesome.io
/// - parameter unicode: An icon unicode from fontawesome.io.
func setFontAwesome(unicode :String){
setFontAwesome(unicode: unicode, color:self.textColor)
}
/// UILabel extension, which allows to set the icon from fontawesome.io
/// - parameter unicode: An icon unicode from fontawesome.io.
/// - parameter color: The color of the text.
func setFontAwesome(unicode :String, color :UIColor){
let charAsInt = Int(unicode, radix: 16)!
let uScalar = UnicodeScalar(charAsInt)
let strUnicode = String(UnicodeScalar(uScalar!))
self.font = (self.frame.width > self.frame.height) ?
UIFont(name: nameFont, size: self.frame.height) :
UIFont(name: nameFont, size: self.frame.width);
self.text = strUnicode
self.textColor = color
}
}
extension UIImageView {
/// UIImageView extension, which allows to set the icon into the UIImageView.image from fontawesome.io
/// - parameter unicode: An icon unicode from fontawesome.io.
/// - parameter color: The color of the image.
func setFontAwesome(unicode: String, color: UIColor){
let textViewCopy = UITextView(frame: self.frame)
textViewCopy.textColor = color
textViewCopy.font = (self.frame.width > self.frame.height) ?
UIFont(name: nameFont, size: self.frame.height) :
UIFont(name: nameFont, size: self.frame.width);
let charAsInt = Int(unicode, radix: 16)!
let uScalar = UnicodeScalar(charAsInt)
let strUnicode = String(UnicodeScalar(uScalar!))
textViewCopy.text = strUnicode
UIGraphicsBeginImageContextWithOptions(textViewCopy.bounds.size, false, UIScreen.main.scale)
textViewCopy.drawHierarchy(in: textViewCopy.bounds, afterScreenUpdates: true)
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
self.image = image
}
}
extension UIButton {
/// UIButton extension, which allows to set the icon as title from fontawesome.io
/// - parameter unicode: An icon unicode from fontawesome.io.
func setFontAwesome(unicode :String){
setFontAwesome(unicode: unicode, color:UIColor.black )
}
/// UIButton extension, which allows to set the icon as title from fontawesome.io
/// - parameter unicode: An icon unicode from fontawesome.io.
/// - parameter color: The color of the title.
func setFontAwesome(unicode :String, color :UIColor){
setFontAwesome(unicode: unicode, color:UIColor.black, forState:UIControlState.normal)
}
/// UIButton extension, which allows to set the icon as title from fontawesome.io
/// - parameter unicode: An icon unicode from fontawesome.io.
/// - parameter color: The color of the title.
/// - parameter forState: The UIControlState of the UIButton.
func setFontAwesome(unicode :String, color :UIColor, forState: UIControlState){
let charAsInt = Int(unicode, radix: 16)!
let uScalar = UnicodeScalar(charAsInt)
let strUnicode = String(UnicodeScalar(uScalar!))
self.titleLabel?.font = (self.frame.width > self.frame.height) ?
UIFont(name: nameFont, size: self.frame.height) :
UIFont(name: nameFont, size: self.frame.width);
self.setTitle(strUnicode, for: forState)
self.setTitleColor(color, for: forState)
}
}
extension UIBarButtonItem {
/// UIBarButtonItem extension, which allows to set the icon as title from fontawesome.io
/// - parameter unicode: An icon unicode from fontawesome.io.
func setFontAwesome(unicode :String){
setFontAwesome(unicode: unicode, size:40)
}
/// UIBarButtonItem extension, which allows to set the icon as title from fontawesome.io
/// - parameter unicode: An icon unicode from fontawesome.io.
/// - parameter size: The UIFont size.
func setFontAwesome(unicode :String, size: CGFloat){
setFontAwesome(unicode: unicode, size: size, forState: .normal)
}
/// UIBarButtonItem extension, which allows to set the icon as title from fontawesome.io
/// - parameter unicode: An icon unicode from fontawesome.io.
/// - parameter size: The UIFont size.
/// - parameter forState: The UIControlState of the UIBarButtonItem.
func setFontAwesome(unicode :String, size: CGFloat, forState: UIControlState){
let charAsInt = Int(unicode, radix: 16)!
let uScalar = UnicodeScalar(charAsInt)
let strUnicode = String(UnicodeScalar(uScalar!))
let attributes = [NSFontAttributeName: UIFont(name: nameFont, size: size)!] as [String: Any]
self.setTitleTextAttributes(attributes, for: forState)
self.title = strUnicode
}
}
extension UITabBarItem {
/// UITabBarItem extension, which allows to set the icon as title from fontawesome.io
/// - parameter unicode: An icon unicode from fontawesome.io.
func setFontAwesome(unicode :String){
setFontAwesome(unicode: unicode, size:40)
}
/// UIBarButtonItem extension, which allows to set the icon as title from fontawesome.io
/// - parameter unicode: An icon unicode from fontawesome.io.
/// - parameter size: The UIFont size.
func setFontAwesome(unicode :String, size: CGFloat){
setFontAwesome(unicode: unicode, size: size, forState: .normal)
}
/// UIBarButtonItem extension, which allows to set the icon as title from fontawesome.io
/// - parameter unicode: An icon unicode from fontawesome.io.
/// - parameter size: The UIFont size.
/// - parameter forState: The UIControlState of the UIBarButtonItem.
func setFontAwesome(unicode :String, size: CGFloat, forState: UIControlState){
let charAsInt = Int(unicode, radix: 16)!
let uScalar = UnicodeScalar(charAsInt)
let strUnicode = String(UnicodeScalar(uScalar!))
let attributes = [NSFontAttributeName: UIFont(name: nameFont, size: size)!] as [String: Any]
self.setTitleTextAttributes(attributes, for: forState)
self.title = strUnicode
}
}