TYIconFontTool
示例
要运行示例项目,请先克隆仓库,然后从 Example 目录中运行 pod install
要求
安装
TYIconFontTool 可通过 CocoaPods 获得。要安装,只需将以下行添加到您的 Podfile 中:
pod 'TYIconFontTool'
将您从 IconFont 平台下载的字体文件 (.ttf) 添加到工程中;
打开 Info.plist 文件,增加一个新的 Array 类型的键,键名设置为 UIAppFonts(由应用提供的字体),增加字体的文件名:“iconfont.ttf”
注意:代码中使用的字体名为 iconfont
,上面的文件名和字体名必须保持一致
使用
通过文本的方式设置
UIButton
在自动布局时,文本显示会有内间距,如不希望有间距,在自动布局时固定按钮大小。
#import <TYIconFontTool/NSAttributedString+TYIconFont.h>
NSAttributedString *loveIconStr = [NSAttributedString ty_attributedStringWithIcon:DTIconfontLoveIcon
fontSize:24
color:[UIColor blackColor]];
[self.btn1 setAttributedTitle:loveIconStr forState:UIControlStateNormal];
或
#import <TYIconFontTool/UIView+TYIconFont.h>
[self.btn1 ty_setIcon:DTIconfontLoveIcon
fontSize:24.f
normalColor:[UIColor purpleColor]
highlightedColor:[UIColor cyanColor]];
深色模式
/// 通过设置 font 属性的方式显示 icon,支持 dark mode
- (void)ty_setDynamicIcon:(NSString *)iconName
fontSize:(CGFloat)fontSize
normalColor:(UIColor *)normalColor
highlightedColor:(UIColor *)highlightedColor;
UILabel
#import <TYIconFontTool/NSAttributedString+TYIconFont.h>
NSAttributedString *loveIconStr = [NSAttributedString ty_attributedStringWithIcon:DTIconfontLoveIcon
fontSize:24
color:[UIColor blackColor]];
self.label1.attributedText = loveIconStr;
或
#import <TYIconFontTool/UIView+TYIconFont.h>
[self.label2 ty_setIcon:DTIconfontLoveIcon fontSize:24.f color:[UIColor orangeColor]];
深色模式
/// 通过设置 font 属性的方式显示 icon,支持 dark mode
- (void)ty_setDynamicIcon:(NSString *)iconName
fontSize:(CGFloat)fontSize
color:(UIColor *)color;
通过图片设置
绘制图片
#import <TYIconFontTool/UIImage+TYIconFont.h>
UIImage *loveIcon = [UIImage ty_imageWithIcon:DTIconfontLoveIcon
imageSize:CGSizeMake(24, 24)
fontSize:24
tintColor:[UIColor redColor]
backgroundColor:[UIColor grayColor]];
UIImageView
设置图片
#import <TYIconFontTool/UIView+TYIconFont.h>
[self.imageView2 ty_setImageWithIcon:DTIconfontLoveIcon
imageSize:CGSizeMake(24.f, 24.f)
fontSize:24.f
tintColor:[UIColor whiteColor]
backgroundColor:[UIColor redColor]];
作者
许可证
TYIconFontTool 在 MIT 许可下可用。有关更多信息,请参阅 LICENSE 文件。