摘要:點點iOS項目自己功能較多,致使應用體積也比較大。一個Xcode工程下圖片資源佔用了很大的空間,且若是有些App須要一鍵換膚功能,呵呵,不知道得作多少圖片。每套圖片還須要設置1x@,2x@,3x@等javascript
IconFont技術起源於Web領域的Web Font技術。隨着時間的推移,網頁設計愈來愈漂亮。可是電腦預裝的字體遠遠沒法知足設計者的要求,因而Web Font技術誕生了。一個英文字庫並不大,經過網絡下載字體,完成網頁的顯示。有了Web Font技術,大大提高了設計師的發揮空間。html
網頁設計中圖標須要適配多個分辨率,每一個圖標須要佔用一次網絡請求。因而有人想到了用Web Font的方法來解決這兩個問題,就是IconFont技術。將矢量的圖標作成字體,一次網絡請求就夠了,能夠保真縮放。解決這個問題的另外一個方式是圖片拼合的Sprite圖。java
Web領域使用IconFont相似的技術已經多年,當我在15年接觸BootStrap的時候Font Awesome技術大行其道。最近IconFont技術在iOS圖片資源方面得以應用,最近有點時間本身研究整理了一番,在此記錄學習點滴。git
純色icon
網上說了一大堆如何製做IconFont的方法,在此不作討論。github
初步嘗試使用xcode
NSMutableAttributedString *attributedStr = [[NSMutableAttributedString alloc] initWithString:@"\U0000e696 \U0000e6ab \U0000e6ac \U0000e6ae"];
[attributedStr addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0, 1)];
[attributedStr addAttribute:NSForegroundColorAttributeName value:[UIColor orangeColor] range:NSMakeRange(3, 1)];
[attributedStr addAttribute:NSForegroundColorAttributeName value:[UIColor blackColor] range:NSMakeRange(9, 1)];
self.label.attributedText = attributedStr;
[self.view addSubview:self.label];
pragma mark - getter and setter
-(UILabel *)label{
if (!_label) {
_label = [[UILabel alloc] initWithFrame:CGRectMake(100, 100, BoundWidth-200, 40)];
_label.font = [UIFont fontWithName:@"iconfont" size:24];
_label.textColor = [UIColor purpleColor];
}
return _label;
}複製代碼
利用IconFont生成1個UIImage只須要LBPIconFontmake(par1, par2, par3),par1:iconfont的unicode值;par2:圖片大小;par3:圖片的顏色值。網絡
其中,LBPIconFontmake是一個宏,#define LBPIconFontmake(text,size,color) [[LBPFontInfo alloc] initWithText:text withSize:size andColor:color]。學習
self.latestImageView.image = [UIImage iconWithInfo:LBPIconFontmake(@"\U0000e6ac", 60, @"000066") ];複製代碼