[英]how to add custom icons using fontAwesome to UIbutton in swift 4
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. 我目前正在开发一个IOS应用程序,我想使用FontAwesome添加按钮标签或图标我已经在我的项目中成功安装了cocoapods,现在我不知道使用cocoapods添加库FontAwesome(更多信息: - 在此处输入链接描述 )我的IOS应用程序中的图标。
thanks 谢谢
There are many ways of using font awesome icons in an iOS application. 在iOS应用程序中使用字体真棒图标有很多种方法。 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. fa-close
和f00d
( 参见示例代码 )。在我的情况下,我有一个类我有所有字体真棒图标字符串和另一个接受字体真棒类字符串并返回相应的Unicode字符串的类。 You can search your desired font awesome string and Unicode here 您可以在此处搜索所需的字体真棒字符串和Unicode
Step1 步骤1
In this example, I have created an extension which returns the Unicode string. 在这个例子中,我创建了一个返回Unicode字符串的扩展。
extension String {
func fontAwesomeString(name: String) -> String {
switch name {
case "fa-close":
return "\u{f00d}"
default: // manage exhaustive case accordingly
}
}
}
Step2 第2步
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. 或者,如果您不想组织源代码,可以直接使用Unicode创建属性字符串并设置为attributedText属性。
Note: You might be required to make changes in above code snippet. 注意:您可能需要在上面的代码段中进行更改。 As I have written for Swift 4.0
正如我为Swift 4.0编写的那样
Using cocoa pods 使用可可豆荚
Once you installed the pod library, you call the appropriate methods shown in the example as below 安装pod库后,可以调用示例中显示的相应方法,如下所示
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 如果你还没有安装CocoaPods,这些是很好的说明: https ://stackoverflow.com/a/25257238/8534588
pod 'FontAwesome.swift'
pod 'FontAwesome.swift'
pod install
pod install
Add import FontAwesome_swift
in your code file 在代码文件中添加
import FontAwesome_swift
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:- 第2步:将所有.otf和.swift文件拖放到项目中第3步:导入FontAwesome_swift第4步:使用以下代码: -
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. 我已经在Objective C中完成了这个,所以希望这会有用。 The procedure will be same but you need to convert the Objective C code to Swift.
过程将是相同的,但您需要将Objective C代码转换为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
因此,为了在Objective C中执行此操作,您可以按照此链接中提到的步骤手动将
FontAwesome
添加到项目中而不使用CocoaPod
,如果您有兴趣不使用CocoaPod
Manually Add FontAwesome 手动添加FontAwesome
From that Github project takeout the NSString
category class, ie NSString+FontAwesome
从那个Github项目中取出
NSString
类别类,即NSString+FontAwesome
You need to add the fontawesome-webfont.ttf in resource folder as well 您还需要在资源文件夹中添加fontawesome-webfont.ttf
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. 注意:对于我来说,在我添加上面链接中提到的NSString Category类之后出现错误,如果您遇到像重复定义这样的问题,那么只需重命名那些不满足变量命名约定的枚举常量。 (For me some of the enum constant were using hypen(-) i replaced those with underscore(_)).
(对我来说,一些枚举常量使用的是hypen( - )我用下划线(_)代替了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.