适用于iOS的图标字体库。目前支持 Font-Awesome、Foundation图标、Zocial、和 ionicons。
目前 Font AwesomeKit 支持了 6 种不同的图标字体。
感谢 NSAttributedString
使得 API 更加简洁和面向对象。全息 NSAttributedString
!
请注意,FontAwesome 在最近的 4.0 版本中重命名了许多图标,如果您使用的是 Font AwesomeKit 2.1 版本,请确保相应地更改代码。
#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 中删除了一个子规范,请删除 Xcode 组织者中的 Xcode 导出数据(使用命令+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 扩展参考
一些属性对图标字体显然没有意义,如 NSLigatureAttributeName
和 NSKernAttributeName
。您不应使用这些属性,否则您的应用程序可能会崩溃。如果您想更改图标的尺寸,不应该设置 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)
返回给定键的属性值。
设置属性后,您可以通过调用 [starIcon attributedString]
或 starIcon.attributedString()
来获取富文本字符串。
因此,您可以使用一行代码使用图标在一个标签上
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)
相同的方法也可以应用于标签栏或分段控制。
堆叠图标是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
如果您遇到奇特的崩溃,请检查已知问题。
请参阅CHANGES.md。
FontAwesomeKit可在MIT许可下使用。请参阅LICENSE文件以获取更多信息。虽然不需要归属,但非常感激。
请注意,每个图标字体都有自己的许可协议。