iconfont是將圖片以字體的形式展示的一種方法谐鼎,清晰易縮放而且文件體積小,可以直接通過修改字體顏色和大小來改變圖片大小趣惠,很方便狸棍。同時也是因為這個原因只能支持純色圖片。不過對于iOS來說味悄,減少了各種@2x草戈、@3x的煩惱,還是很方便的侍瑟。官方文檔不是很詳細(xì)唐片,下面說說如何使用,希望能幫到大家少走彎路涨颜。
第一步:點擊下載到本地费韭,將文件中的字體文件(.ttf)添加到工程中;
第二步:打開Info.plist文件庭瑰,增加一個新的Array類型的鍵星持,鍵名設(shè)置為UIAppFonts(Fonts provided by application),增加字體的文件名:“iconfont.ttf“弹灭;
第三步:使用
iconfont有兩種使用方法督暂,最常用的就是用label來展示
UILabel* label = [[UILabelalloc] initWithFrame:self.view.bounds];
UIFont*iconfont = [UIFontfontWithName:@"uxIconFont"size:34];
label.font = iconfont;
label.text = @"\U00003439 \U000035ad \U000035ae \U000035af \U000035eb \U000035ec";
label.textColor = BLACKCOLOR;
[self.view addSubview: label];
fontname就是上圖中藍(lán)色標(biāo)記位置 fontfamily羹令,text內(nèi)存為 \U0000加上上圖紅色標(biāo)記位置代碼的后四位,如上圖個人 text就為 \U0000e662
有的時候不能使用label损痰,只能用imageview福侈,比如tabbaritem,這個時候就需要把icon轉(zhuǎn)換為image
+ (UIImage*)imageWithIcon:(NSString*)icon
iconColor:(UIColor*)color
iconSize:(CGFloat)size{
CGFloat scale = [UIScreen mainScreen].scale;
CGFloat realSize = size * scale;//屏幕分辨率調(diào)整圖片大小
UIFont *font = [UIFont fontWithName:@"iconfont" size:realSize];
UIGraphicsBeginImageContext(CGSizeMake(realSize, realSize));//設(shè)置圖片尺寸
CGContextRef context = UIGraphicsGetCurrentContext();
if ([icon respondsToSelector:@selector(drawAtPoint:withAttributes:)]) {
[icon drawAtPoint:CGPointZero withAttributes:@{NSFontAttributeName:font, NSForegroundColorAttributeName: color}];
} else {
CGContextSetFillColorWithColor(context, color.CGColor);
[icon drawAtPoint:CGPointMake(0, 0) withAttributes:@{NSFontAttributeName:font}];
}
UIImage *image = [UIImage
imageWithCGImage:UIGraphicsGetImageFromCurrentImageContext().CGImage
scale:scale orientation:UIImageOrientationUp];
UIGraphicsEndImageContext();
return image;
}
6月29日補
?如何寫法沒錯 卻一直顯示問號? 這個時候一般是字體文件沒加載上的問題? 首先檢查 plist文件里有沒寫錯單詞卢未, 然后BuildPhases--BundleResources里有無文件? 如果都沒問題
拖入字體文件時使用這些選項
如果還是顯示問號
讓讓 我要放大招了
建立一個XIB\SB文件? 找一個label? 設(shè)置如下屬性
然后再運行 就OK了