ios 如何在 Xcode 项目中将 Font Awesome 与 Objective-C 或 Swift 集成和使用?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10569878/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 18:21:37  来源:igfitidea点击:

How to integrate and use Font Awesome with Objective-C or Swift in an Xcode project?

iosobjective-cswiftunicodefont-awesome

提问by adit

So I am trying to use this font http://fortawesome.github.com/Font-Awesome/. I've added the font as a resource and put it in the plist file. Here's how I am using it:

所以我试图使用这种字体http://fortawesome.github.com/Font-Awesome/。我已将字体添加为资源并将其放入 plist 文件中。这是我如何使用它:

[homeFeedButton.titleLabel setFont:[UIFont fontWithName:@"fontawesome" size:8]];
NSString *str = [NSString stringWithFormat:@"%C", @"\f030"];

However this doesn't work. Does anyone know how to use this font in an Xcode project?

然而这行不通。有谁知道如何在 Xcode 项目中使用这种字体?

采纳答案by CodaFi

It is because your +[NSString stringWithFormat:]method contains the literal for a unichar, not an NSString, which is an object that uses %@, which is beside the point because literal'ing a literal is redundant.

这是因为您的+[NSString stringWithFormat:]方法包含 unichar 的文字,而不是 NSString,它是一个使用 %@ 的对象,这是无关紧要的,因为文字是多余的。

回答by King-Wizard

In Swift:

斯威夫特

Font Awesome Cheatsheet.

字体真棒备忘单。

Tutorial on how to integrate the font called "Font Awesome" in your Xcode project.

关于如何在 Xcode 项目中集成名为“Font Awesome”的字体的教程。

Common Mistakes With Adding Custom Fonts to Your iOS App

常见误区与添加自定义字体到您的iOS应用

let label = UILabel(frame: CGRectMake(0, 0, 100, 100))
label.font = UIFont(name: "FontAwesome", size: 40)
let myChar: UniChar = 0xF180
label.text = String(format: "%C", myChar)
self.view.addSubview(label)

回答by Danielle

BEST solution for FA with XCode:

使用 XCode 进行 FA 的最佳解决方案:

  1. Copy the icon you want from http://fontawesome.io/cheatsheet/(I mean mark and copy the icon itself):
  1. http://fontawesome.io/cheatsheet/复制你想要的图标(我的意思是标记并复制图标本身):

enter image description here

在此处输入图片说明

  1. Then add a label to your storyboard, and on its properties:
  1. 然后将标签添加到您的故事板,并在其属性上:

enter image description here

在此处输入图片说明

  • RED - Choose FontAwesome as family
  • GREEN - Set the size you want
  • BLUE - Paste here what you copied in the 1st step. (dont worry about the question mark - in the view you should see it properly).
  • RED - 选择 FontAwesome 作为系列
  • 绿色 - 设置您想要的尺寸
  • 蓝色 - 将您在第一步中复制的内容粘贴到此处。(不要担心问号 - 在视图中您应该正确地看到它)。

That's it.

就是这样。

If you need to change the icon in the code - you can paste 1st step inside your code too:enter image description here

如果您需要更改代码中的图标 - 您也可以在代码中粘贴第一步:在此处输入图片说明

回答by ocodo

Not sure you ever got this working properly, but there's now a couple of nice projects on github:

不确定你有没有让它正常工作,但现在 github 上有几个不错的项目:

https://github.com/alexdrone/ios-fontawesome- which gives you a category for NSString which offers basic help using FontAwesome.

https://github.com/alexdrone/ios-fontawesome- 它为您提供了一个 NSString 类别,它提供了使用 FontAwesome 的基本帮助。

and https://github.com/leberwurstsaft/FontAwesome-for-iOSwhich gives you a NSString category with fontAwesomeIconStringForIconIdentifierand also an UIImageViewsubclass: FAImageView

https://github.com/leberwurstsaft/FontAwesome-for-iOS它为您提供了一个 NSString 类别fontAwesomeIconStringForIconIdentifier以及一个UIImageView子类:FAImageView

回答by Jais

You can just use the literal code string by affixing it with \u.

您可以通过将其附加到\u.

e.g. You can use \f030in your iOS app using the following snippet.

例如,您可以使用\f030以下代码段在您的 iOS 应用程序中使用。

[[[UILabel alloc] initWithFrame:CGRectZero] setText:@"\uf030"];

[[[UILabel alloc] initWithFrame:CGRectZero] setText:@"\uf030"];

回答by Néstor

You can also use the library FontAwesome+iOS for iOS

您还可以使用库FontAwesome+iOS for iOS

Then you only need to use this code:

那么你只需要使用这个代码:

label.text = [NSString fontAwesomeIconStringForIconIdentifier:@"icon-github"];
// or:
label.text = [NSString fontAwesomeIconStringForEnum:FAIconGithub];

回答by Kirit Modi

So, I can answer it for Swift & Objective C both. (using Swift 3.0 here)

所以,我可以为 Swift 和 Objective C 都回答。(此处使用 Swift 3.0)

Adding & configuring font-awesome file & Editing Storyboard

添加和配置字体很棒的文件和编辑故事板

Just download font-awesome from here: fontawesome.io and add .ttf to your project

只需从这里下载 font-awesome:fontawesome.io 并将 .ttf 添加到您的项目中

Check the image below for more details...

查看下面的图片了解更多详情...

follow the sequence given in the image

按照图片中给出的顺序

Now the coding part

现在是编码部分

Swift 3.0

斯威夫特 3.0

let iconUniChar: UniChar = 0xf113  
textLabel.text = String(format: "%C", iconUniChar) 

Objective C

目标 C

_textLabel.text = [NSString stringWithFormat:@"%C", 0xf113];

And, if you are still facing the trouble here's the

而且,如果您仍然面临问题,这里是

entire source code

完整的源代码

回答by Sudheer Kumar Palchuri

Below is the code to set image to a UIButton using FontAwesome

下面是使用 FontAwesome 将图像设置为 UIButton 的代码

 UIButton *btnMenu = [UIButton buttonWithType:UIButtonTypeCustom];
 btnMenu.frame = CGRectMake(0, 0, 40, 32);
 [btnMenu setTitle:@"\uf0c9" forState:UIControlStateNormal];
 [btnMenu setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
 [btnMenu.titleLabel setFont:[UIFont fontWithName:@"FontAwesome" size:16]];

回答by Gaurav

NSLog your all font using following code and provide exact name.

使用以下代码 NSLog 您的所有字体并提供确切名称。

 NSArray *familyNames = [[NSArray alloc] initWithArray:[UIFont familyNames]];
    NSArray *fontNames;
    NSInteger indFamily, indFont;
    for (indFamily=0; indFamily<[familyNames count]; ++indFamily)
    {
        NSLog(@"Family name: %@", [familyNames objectAtIndex:indFamily]);
        fontNames = [[NSArray alloc] initWithArray:
                     [UIFont fontNamesForFamilyName:
                      [familyNames objectAtIndex:indFamily]]];
        for (indFont=0; indFont<[fontNames count]; ++indFont)
        {
            NSLog(@"    Font name: %@", [fontNames objectAtIndex:indFont]);
        }
    }

回答by Anton

I was looking for the helper library for Swift3 and found this to be very simple and working solution https://github.com/Syndicode/FontAwesomeKitSwift3. It has animations that explain for novices like me how to include it and use quickly.

我正在寻找 Swift3 的帮助程序库,发现这是一个非常简单且有效的解决方案https://github.com/Syndicode/FontAwesomeKitSwift3。它有动画,可以为像我这样的新手解释如何包含它并快速使用它。