简体   繁体   English

如何在swift 4中使用fontAwesome将自定义图标添加到UI按钮

[英]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应用程序中的图标。

  • I need to know about the procedure of adding FontAwesome using cocoapods, and 我需要知道使用cocoapods添加FontAwesome的过程,以及
  • I need to know if their is another way to add FontAwesome to my project 我需要知道他们是否是将FontAwesome添加到我的项目的另一种方式

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. 您可以根据自己的理解和舒适度选择退出其中任何一个。

Approach 1: 方法1:

Writing your own logic 编写自己的逻辑

  • Add font awesome files into your compile source and make sure added properly (See the attached pictures) 将字体真棒文件添加到您的编译源中并确保正确添加(参见附图)
  • Keep Unicode string of any font-awesome icon(In the example code I am taking the close icon reference. Its font awesome class and Unicode texts are 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. 保留Unicode字符串的任何字体 - 真棒图标(在示例代码中我采用关闭图标引用。它的字体很棒的类和Unicode文本分别是fa-closef00d参见示例代码 )。在我的情况下,我有一个类我有所有字体真棒图标字符串和另一个接受字体真棒类字符串并返回相应的Unicode字符串的类。
  • Create an attributed string with that Unicode string and set it to attributedText property ( See the example code below ). 使用该Unicode字符串创建属性字符串并将其设置为attributedText属性( 请参阅下面的示例代码 )。

You can search your desired font awesome string and Unicode here 您可以在此处搜索所需的字体真棒字符串和Unicode

你的字体真棒文件看起来像这样的东西

确保将这些字体真棒文件添加到构建阶段源

Code Example 代码示例

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编写的那样

Approach 2: 方法2:

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

Install FontAwesome with CocoaPods: 使用CocoaPods安装FontAwesome:

  1. Open up Podfile which is in the same folder as your .xcodeproj file 打开与.xcodeproj文件位于同一文件夹中的Podfile
  2. Add this line to Podfile to install FontAwesome.swift: pod 'FontAwesome.swift' 将此行添加到Podfile以安装FontAwesome.swift: pod 'FontAwesome.swift'
  3. Save changes to Podfile 将更改保存到Podfile
  4. Open up Terminal in the folder that contains Podfile and run the command pod install 在包含Podfile的文件夹中打开终端并运行命令pod install

How to use FontAwesome icons: 如何使用FontAwesome图标:

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM