I am currently working on an IOS application and i want to add button labels or icon using FontAwesome i have successfully installed cocoapods in my project and now i have no idea of using cocoapods for adding library FontAwesome(More info:-enter link description here) icons in my IOS Application.
thanks
There are many ways of using font awesome icons in an iOS application. You can opt out any one of them according to your understanding and comfort.
Writing your own logic
fa-close
and f00d
respectively (See example code). In my case, I have a class where I have all font awesome icon string and another class which accept font awesome class string and returns the appropriate Unicode string.You can search your desired font awesome string and Unicode here
Step1
In this example, I have created an extension which returns the Unicode string.
extension String {
func fontAwesomeString(name: String) -> String {
switch name {
case "fa-close":
return "\u{f00d}"
default: // manage exhaustive case accordingly
}
}
}
Step2
Call above method by passing the appropriate font awesome string.
let iconUnicodeText = String.fontAwesomeString(name: "fa-close")
let iconAttributed = NSMutableAttributedString(string: iconUnicodeText)
self.iConLabel.attributedText = iconAttributed // iConLabel is a control type of UIlabel.
Or if you don't want to organise your source code you can directly create an attributed string with Unicode and set to attributedText property.
Note: You might be required to make changes in above code snippet. As I have written for Swift 4.0
Using cocoa pods
Once you installed the pod library, you call the appropriate methods shown in the example as below
yourButton.titleLabel?.font = UIFont.fontAwesome(ofSize: 30, style: .brands)
yourButton.setTitle(String.fontAwesomeIcon(name:. gitgub), for : .normal) // you may change icon type with your desired one
If you have not already installed CocoaPods, these are good instructions: https://stackoverflow.com/a/25257238/8534588
pod 'FontAwesome.swift'
pod install
Add import FontAwesome_swift
in your code file
Example code:
let image = UIImage.fontAwesomeIcon(name: .checkCircle, style: .solid, textColor: UIColor.black, size: CGSize(width: 40, height: 40))
Use like below:-
Step1 : Add framework like below image
Step 2:Drag and drop all .otf and .swift files into your project Step 3:import FontAwesome_swift Step 4: Use below code:-
let imageView = UIImageView(frame: CGRect(x: 80.0, y: 80.0, width: 50, height: 50))
imageView.image = UIImage.fontAwesomeIcon(name: .github, style: .brands, textColor: .black, size: CGSize(width:40,height:40))
self.view.addSubview(imageView)
Result:-
I have done this in Objective C, So hope this would be useful. The procedure will be same but you need to convert the Objective C code to Swift.
So for do this in Objective C,
You can follow the step mentioned in this link to add the FontAwesome
manually to your project without using CocoaPod
, If you are interested not to use CocoaPod
Manually Add FontAwesome
From that Github project takeout the NSString
category class, i.e NSString+FontAwesome
You need to add the fontawesome-webfont.ttf in resource folder as well
NB: For me error was coming after I add NSString Category class mentioned there in the above link, If you are facing the issue like duplicate definition then just rename those enum constants those are not satisfying the variable naming convention. (For me some of the enum constant were using hypen(-) i replaced those with underscore(_)).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With