SZFontAwesomeKit 2.2.3

SZFontAwesomeKit 2.2.3

测试已测试
语言语言 Obj-CObjective C
许可协议 MIT
发布最新发布2016年11月

Jaja Yting 维护。



  • Pride Chung

FontAwesomeKit

iOS 的图标字体库。目前支持 Font-Awesome,Foundation 图标,Zocial 和 ionicons。

2.2 版本重要更改

不止是优秀。新增图标字体

当前 FontAwesomeKit 支持 6 种不同的图标字体。

API 重构,利用 NSAttributedString

多亏了 NSAttributedString,API 更加清洁且面向对象。向 NSAttributedString 致敬!

关于 FontAwesome 的说明

请注意,FontAwesome 在最近的 4.0 版本中对其许多图标进行了重命名,如果您使用的是 FontAwesomeKit 2.1 版本,请确保相应地更改代码。

安装

要求

  • Xcode 5
  • iOS 6.0+
  • tvOS 9.0
  • 启用 ARC
  • CoreText 框架

导入头文件

#import FontAwesomeKit/FontAwesomeKit.h 如果您已安装所有可用的图标字体。

或者导入您使用子规格安装的图标字体

#import FontAwesomeKit/FAKFontAwesome.h
#import FontAwesomeKit/FAKFoundationIcons.h
#import FontAwesomeKit/FAKZocial.h
#import FontAwesomeKit/FAKIonIcons.h #import FontAwesomeKit/FAKOcticons.h #import FontAwesomeKit/FAKMaterialIcons.h

重要:

如果您在 Podfile 中删除了一个子 spec,请删除 Xcode 的衍生数据(按 command+shift+2 打开组织者)。否则,Xcode 会持续将应该被删除的字体文件复制到应用程序包中。

手动安装

下载源代码,然后将 FontAwesomeKit 文件夹拖入您的项目中,并将 CoreText 框架添加到您的项目中。

示例用法

创建图标

FAKFontAwesome *starIcon = [FAKFontAwesome starIconWithSize:15];
FAKFoundationIcons *bookmarkIcon = [FAKFoundationIcons bookmarkIconWithSize:15];
FAKZocial *twitterIcon = [FAKZocial twitterIconWithSize:15];  
FAKIonIcons *mailIcon = [FAKIonIcons ios7EmailIconWithSize:48];
FAKOcticons *repoIcon = [FAKOcticons repoIconWithSize:48];
FAKMaterialIcons *androidIcon = [FAKMaterialIcons androidIconWithSize:48];
let starIcon = FAKFontAwesome.starIcon(withSize: 15)
let bookmarkIcon = FAKFoundationIcons.bookmarkIcon(withSize: 15)
let twitterIcon = FAKZocial.twitterIcon(withSize: 15)
let mailIcon = FAKIonIcons.ios7EmailIcon(withSize: 48)
let repoIcon = FAKOcticons.repoIcon(withSize: 48)
let androidIcon = FAKMaterialIcons.androidIcon(withSize: 48)

现在您可以使用这些类方法并通过传入字体大小来创建图标,而不是使用常量来查找图标。相应的图标字体将自动为您设置。

使用标识符创建图标

现在可以通过使用标识符来创建图标。检查每份文档以获取适当的标识符。同时请确保您使用了一个现有的标识符,否则方法将返回 nil 并设置一个错误。

NSError *error;
FAKFontAwesome *starIcon = [FAKFontAwesome  iconWithIdentifier:@"fa-star" size:15 error:error];
FAKFoundationIcons *bookmarkIcon = [FAKFoundationIcons iconWithIdentifier:@"fi-bookmark" size:15 error:error];
FAKZocial *twitterIcon = [FAKZocial iconWithIdentifier:@"zocial.twitter" size:15 error:error];
FAKIonIcons *mailIcon = [FAKIonIcons iconWithIdentifier:@"ion-ios-email" size:48 error:error];
let starIcon: FAKFontAwesome?
do {
  starIcon = try FAKFontAwesome.init(identifier: "er", size: 15)
} catch let error as NSError {
  print(error.localizedDescription)
}

为图标设置属性

[starIcon addAttribute:NSForegroundColorAttributeName value:[UIColor
whiteColor]];
starIcon.addAttribute(NSForegroundColorAttributeName, UIColor.white)

NSAttributedString 在幕后完成了所有魔法。因此,您可以为图标设置 NSAttributedString 支持的属性。有关所有可用属性,请参阅 NSAttributedString UIKit 扩展参考

重要:

一些属性对图标字体来说似乎没有意义,如 NSLigatureAttributeNameNSKernAttributeName。您不应使用这些属性,否则您的应用可能会崩溃。如果您想改变图标的大小而不是设置 NSFontAttributeName 的值,那么请设置它 的 iconFontSize 属性。

设置或获取属性的其他方法

这些方法实际上是标准 NSAttributedString API 的简称版本,应该相当直接。

[starIcon setAttributes:attributes]; starIcon.setAttributes(attributes) 使用字典设置属性,如果有相同键的不同值,则将覆盖当前属性。

[starIcon removeAttribute:NSForegroundColorAttributeName]; starIcon.removeAttribute(NSForegroundColorAttributeName) 通过名称移除属性。

[starIcon attributes]; starIcon.attributes() 返回一个包含图标属性值的字典。

[starIcon attribute:NSForegroundColorAttributeName]; starIcon.attribute(NSForegroundColorAttributeName) 针对指定键返回属性值。

获取 attributed 字符串

在设置完属性后,您可以通过调用 [starIcon attributedString] starIcon.attributedString() 来获取 attributed 字符串。

因此,您可以用一行代码在标签上使用图标:

self.label.attributedText = [starIcon attributedString]; self.label.attributedText = starIcon.attributedString()

您不需要设置标签的 font 属性,这已经被处理了。

在图像上绘制图标

基本绘图

直接获取属性字符串,您可以将图标绘制到图像上,如下所示

UIImage *iconImage = [starIcon imageWithSize:CGSizeMake(15, 15)];

let iconImage = starIcon.image(with: CGSize(width: 15, height: 15))

这将使用您设定的属性来绘制图像,您只需指定图像大小即可。

绘制偏移

默认情况下,图标水平和垂直居中。我相信这符合您的99%。但是,如果图标没有居中,您可以像这样设置图标的 drawingPositionAdjustment 属性

starIcon.drawingPositionAdjustment = UIOffsetMake(2, 2);

背景颜色

您可以像这样设置图像的背景颜色

starIcon.drawingBackgroundColor = [UIColor blackColor];

starIcon.drawingBackgroundColor = UIColor.black

默认情况下,背景是透明的。正如其名所示,此属性只在绘图时生效。您可以为创建渐变背景指定渐变色,详细请查看示例项目。

不支持属性字符串的控件

一些UI元素没有属性字符串属性,使用图像可能是一个更好的选择。以UIBarButtonItem为例。

FAKFontAwesome *cogIcon = [FAKFontAwesome cogIconWithSize:20];
[cogIcon addAttribute:NSForegroundColorAttributeName value:[UIColor whiteColor]];
UIImage *leftImage = [cogIcon imageWithSize:CGSizeMake(20, 20)];
cogIcon.iconFontSize = 15;
UIImage *leftLandscapeImage = [cogIcon imageWithSize:CGSizeMake(15, 15)];
self.navigationItem.leftBarButtonItem =
[[UIBarButtonItem alloc] initWithImage:leftImage
                   landscapeImagePhone:leftLandscapeImage
                                 style:UIBarButtonItemStylePlain
                                target:nil
                                action:nil];
let cogIcon = FAKFontAwesome.cogIcon(withSize: 20)
cogIcon?.addAttribute(NSForegroundColorAttributeName, value: UIColor.white)
let leftImage = cogIcon?.image(with: CGSize(width: 20, height: 20))
cogIcon?.iconFontSize = 15
let leftLandscapeImage = cogIcon?.image(with: CGSize(width: 15, height: 15))
self.navigationItem.leftBarButtonItem = UIBarButtonItem(
  image: leftImage,
  landscapeImagePhone: leftLandscapeImage,
  style: .plain,
  target: nil,
  action: nil)

相同的方法可以应用于标签栏或分段控件。

生成堆叠图标图像(自V2.1.5以来)

Stacked icons是Font-Awesome的一个功能,现在已移植到FontAwesomeKit。您可以为堆叠的多个图标生成一个图像。

[UIImage imageWithStackedIcons:@[[FAKFontAwesome twitterIconWithSize:35], [FAKFontAwesome squareOIconWithSize:70]],
                     imageSize:CGSizeMake(80, 80)];
let image = UIImage(stackedIcons: [FAKFontAwesome.twitterIcon(withSize: 35), FAKFontAwesome.squareOIcon(withSize: 70)], imageSize: CGSize(width: 80, height: 80))

数组中的第一个图标将被绘制在底部。

更多示例

请克隆主仓库并查看示例项目,所有内容都在其中,所有公开的方法和属性都已记录。如有任何问题,请随时提交问题。

使用自定义图标字体

您可以使用诸如fontastic.me之类的网络应用程序生成自己的图标字体以减少字体文件大小。在这种情况下,您需要实现自己的FAKIcon子类,以下是一个完整的演示:PrideChung / FontAwesomeKitCustomFont

已知问题

如果您遇到奇怪的崩溃,请查看Known Issues

更改日志

CHANGES.md

贡献者

许可

FontAwesomeKit可在MIT许可下获得。有关更多信息,请参阅LICENSE文件。不需要归因,但将非常感激。

请注意,每个图标字体都有自己的许可协议。