鍦╥OS鐨勯」鐩紑鍙戜腑缁忓父閬囧埌闇�瑕佷娇鐢ㄤ竴浜涜嚜瀹氫箟鐨勫瓧浣撴枃浠讹紝姣斿浠垮畫_GB2312銆佹柟姝e皬鏍囧畫_GBK绛夈�備箣鍓嶆垜浠负浜嗕娇鐢ㄨ繖浜涜嚜瀹氫箟鐨勫瓧浣擄紝鍦ㄥ簲鐢ㄧ殑璧勬簮鍖呬腑鏀惧叆杩欎簺瀛椾綋鏂囦欢銆傚洜涓哄瓧浣撴枃浠堕�氬父姣旇緝澶э紝鏈夌殑涓�涓瓧搴撳氨杈惧埌10M浠ヤ笂锛堟嬁鏂规灏忔爣瀹媉GBK杩欎釜瀛楀簱鏉ヨ灏辨湁13M涔嬪锛夛紝杩欐牱鎵撳寘鍚庣殑ipa鏂囦欢鐨勪綋绉氨鍙兘浼氬彉寰楀緢澶э紝瀵逛簬鍙湁涓埆鐨勬ā鍧楅渶瑕佺壒娈婄殑瀛椾綋鏍峰紡鐨勫簲鐢ㄦ潵璇村緢涓嶅垝绠楋紝閭d箞鍦╥OS6.0浠ュ悗鑻规灉灏卞紑鏀句簡鍔ㄦ�佸姞杞藉瓧浣撶殑鏉冮檺銆備笅闈㈠氨iOS涓娇鐢ㄥ瓧浣撶殑杩欎袱绉嶆柟寮忚繘琛屼粙缁嶃��
###浣跨敤闈欐�佸瓧浣�
1銆佸皢瀛椾綋鏂囦欢鎷疯礉鍒伴」鐩伐绋嬩腑锛屽湪Info.plist鏂囦欢涓坊鍔燜onts provided by application鐨勯厤缃」锛屽叾涓瘡涓�涓狪tem瀵瑰簲鐨勬槸瀛椾綋鏂囦欢鐨勫悕绉帮紝濡侱S-DIGI.TTF銆�
2銆佷娇鐢ㄦ椂鐩存帴鎸夌収濡備笅鏂瑰紡鍗冲彲锛�
_textLabel1.font = [UIFont fontWithName:@"DS-Digital" size:40];
鏁堟灉濡備笅锛�
3銆佸叾浠栬鏄庯細
+ (UIFont *)fontWithName:(NSString *)fontName size:(CGFloat)fontSize;杩欎釜鏂规硶涓渶瑕佹寚瀹氱殑fontName涓嶆槸鍓嶉潰璁剧疆鐨勫瓧浣撴枃浠剁殑鏂囦欢鍚嶏紝鑰屽簲璇ユ槸瀛椾綋鐨勫悕绉帮紝濡備綍鑾峰彇瀛椾綋鐨勫悕绉板彲浠ラ�氳繃濡備笅鏂瑰紡锛�
锛�1锛夋墦鍗板嚭褰撳墠鎵�鏈夊彲鐢ㄧ殑瀛椾綋锛屾煡鎵惧搴旂殑瀛椾綋鍚嶇О
- (void)printAllFonts
{
聽 聽 NSArray *fontFamilies = [UIFont familyNames];
聽 聽 for (NSString *fontFamily in fontFamilies)
聽 聽 {
聽 聽 聽 聽 NSArray *fontNames = [UIFont fontNamesForFamilyName:fontFamily];
聽 聽 聽 聽 NSLog (@"%@: %@", fontFamily, fontNames);
聽 聽 }
}
锛�2锛夐�氳繃Mac鑷甫鐨勫瓧浣撳唽鏌ョ湅瀛椾綋鐨勫悕绉�
鐩存帴鍙屽嚮瀛椾綋鍗冲彲鎵撳紑瀛椾綋鍐岋紝濡傛灉绯荤粺娌℃湁瀹夎璇ュ瓧浣撴寜鐓ц姹傚畨瑁呭嵆鍙紝鐒跺悗鍙互鍦ㄥ瓧浣撶殑璇︾粏淇℃伅涓壘鍒板搴旂殑瀛椾綋鐨勫悕绉帮細
###浣跨敤鍔ㄦ�佸瓧浣�
####1銆佸姩鎬佷笅杞借嚜瀹氫箟鐨勫瓧浣�
鍦ㄧ綉鏄撴柊闂籭OS瀹㈡埛绔腑鍙互浣跨敤鑷畾涔夌殑瀛椾綋锛屽浜庢湭涓嬭浇鐨勫瓧浣撳彲鍏堜笅杞界劧鍚庡畨瑁呬笅娆″氨鑳借嚜鍔ㄨ缃负璇ュ瓧浣擄紝鏁堟灉濡備笅锛�
涓嬮潰灏辫鍔熻兘绠�鍗曚粙缁嶅疄鐜扮殑姝ラ
#####锛�1锛変笅杞芥寚瀹氱殑瀛椾綋鏂囦欢鍒版湰鍦�
绗竴娆¤繘鍏ヨ椤甸潰浼氳嚜鍔ㄥ埌鏈嶅姟鍣ㄤ笂鑾峰彇鍙娇鐢ㄧ殑瀛椾綋鐨勫垪琛紝绀轰緥濡備笅锛�
[
{
"fontTitle": "鍗庡悍鍦嗕綋",
"regularName": "DFYuanW3-GB",
"boldName": "DFYuanW5-GB",
"author": "鍗庡悍绉戞妧",
"date": "2012-10-11",
"fileUrl": "http://xxxx/font/dfgb_y.zip",
"fileSize": "3.3MB",
"previewUrl": "http://yyyy/font/ios_yuanti.png"
}
]
涓婇潰鐨勫唴瀹规寚鏄庝簡瀛椾綋鐨勫悕绉帮紝涓嬭浇鍦板潃绛変俊鎭紝浠庝笂闈㈢殑鍐呭鍙互鐪嬪嚭涓嬭浇鍥炴潵鐨勫瓧浣撴枃浠舵槸涓�涓獄ip鍘嬬缉鍖咃紝鍐嶄娇鐢ㄥ墠杩橀渶瑕佽繘琛岃В鍘嬪鐞嗐��
######1锛変笅杞藉瓧浣撴枃浠�
- (NSString *)downloadZipFile:(NSString *)fileUrl toPath:(NSString *)path
{
聽 聽 NSError *error = nil;
聽 聽 NSURL *url = [NSURL URLWithString:fileUrl];
聽 聽 NSString *fileName = [url lastPathComponent];
聽 聽 NSData *data = [NSData dataWithContentsOfURL:url options:0 error:&error];
聽 聽 if(!error)
聽 聽 {
聽 聽 聽 聽 NSString *zipPath = [path stringByAppendingPathComponent:fileName];
聽 聽 聽 聽 [data writeToFile:zipPath options:0 error:&error];
聽 聽 聽 聽 if(!error)
聽 聽 聽 聽 {
聽 聽 聽 聽 聽 聽 return zipPath;
聽 聽 聽 聽 }
聽 聽 }
聽 聽 return nil;
}
######2锛夎В鍘媧ip鍘嬬缉鍖�
iOS涓В鍘媧ip鍘嬬缉鏂囦欢闈炲父鏂逛究锛屼娇鐢╖ipArchive杩欎釜寮�婧愰」鐩寜鐓у涓嬬殑鏂瑰紡鍗冲彲蹇�熻В鍘媧ip鏂囦欢銆�
- (NSString *)expandZipFile:(NSString *)src toPath:(NSString *)desc
{
聽 聽 ZipArchive *za = [[ZipArchive alloc] init];
聽 聽 if ([za UnzipOpenFile:src])
聽 聽 {
聽 聽 聽 聽 BOOL ret = [za UnzipFileTo:desc overWrite:YES];//瑙e帇鏂囦欢
聽 聽 聽 聽 if(ret)
聽 聽 聽 聽 {
聽 聽 聽 聽 聽 聽 NSString *zipName = [src lastPathComponent];//鑾峰彇zip鏂囦欢鐨勬枃浠跺悕
聽 聽 聽 聽 聽 聽 [[NSFileManager defaultManager] removeItemAtPath:zipPath error:nil];//鍒犻櫎zip鍘嬬缉鍖�
聽 聽 聽 聽 聽 聽 zipName = [zipName substringToIndex:[zipName rangeOfString:@".zip"].location];//鑾峰彇瑙e帇鍒扮殑鏂囦欢澶�
聽 聽 聽 聽 聽 聽 return [self.downloadPath stringByAppendingPathComponent:zipName];
聽 聽 聽 聽 }
聽 聽 }
聽 聽 return nil;
}
ZipArchive椤圭洰鍦板潃锛歨ttps://github.com/mattconnolly/ZipArchive
#####锛�2锛夋敞鍐屾寚瀹氳矾寰勪笅鐨勫瓧浣撴枃浠�
涓嬭浇鍥炴潵鐨勫瓧浣撴枃浠跺鏋滀笉鍋氬鐞嗘槸涓嶈兘鐩存帴浣跨敤鐨勶紝浣跨敤鍓嶉渶瑕佸厛娉ㄥ唽鐒跺悗鎵嶈兘浣跨敤锛屾敞鍐屾柟寮忓涓嬶細
- (void)registerFont:(NSString *)fontPath
{
聽 聽 NSData *dynamicFontData = [NSData dataWithContentsOfFile:fontPath];
聽 聽 if (!dynamicFontData)
聽 聽 {
聽 聽 聽 聽 return;
聽 聽 }
聽 聽 CFErrorRef error;
聽 聽 CGDataProviderRef providerRef = CGDataProviderCreateWithCFData((__bridge CFDataRef)dynamicFontData);
聽 聽 CGFontRef font = CGFontCreateWithDataProvider(providerRef);
聽 聽 if (! CTFontManagerRegisterGraphicsFont(font, &error))
聽 聽 {
聽 聽 聽 聽 //娉ㄥ唽澶辫触
聽 聽 聽 聽 CFStringRef errorDescription = CFErrorCopyDescription(error);
聽 聽 聽 聽 NSLog(@"Failed to load font: %@", errorDescription);
聽 聽 聽 聽 CFRelease(errorDescription);
聽 聽 }
聽 聽 CFRelease(font);
聽 聽 CFRelease(providerRef);
}
闇�瑕佸厛寮曞叆#import
#####锛�3锛夊垽鏂瓧浣撴槸鍚﹀姞杞�
鍦ㄤ娇鐢ㄥ瓧浣撴枃浠跺墠鏈�濂芥槸鍏堝垽鏂瓧浣撴槸鍚﹀凡缁忚鍔犺浇杩囦簡锛屽垽鏂柟寮忓涓嬶細
- (BOOL)isFontDownloaded:(NSString *)fontName
{
聽 聽 UIFont* aFont = [UIFont fontWithName:fontName size:12.0];
聽 聽 BOOL isDownloaded = (aFont && ([aFont.fontName compare:fontName] == NSOrderedSame || [aFont.familyName compare:fontName] == NSOrderedSame));
聽 聽 return isDownloaded;
}
#####锛�4锛夊叾浠栬鏄�
缁忔祴璇曟敞鍐岃繃鐨勫瓧浣撳湪搴旂敤鍏抽棴鍚庝笅娆″紑鍚簲鐢紝鍒ゆ柇瀛椾綋鏄惁鍔犺浇鏃惰繑鍥炰负NO锛屼负浜嗕繚璇佹甯镐娇鐢ㄩ渶瑕佹瘡娆″惎鍔ㄥ簲鐢ㄧ殑鏃跺�欏厛閬嶅巻涓�閬嶅瓧浣撴枃浠跺す灏嗛噷闈㈢殑瀛椾綋鏂囦欢閮藉啀娆℃敞鍐屼竴閬嶅嵆鍙�傚弬鑰冧唬鐮佸涓嬶細
//娉ㄥ唽fonts鐩綍涓嬮潰鐨勬墍鏈夊瓧浣撴枃浠�
NSArray *ary = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:self.downloadPath error:nil];
for (NSString *p1 in ary)
{
NSString *t1 = [self.downloadPath stringByAppendingPathComponent:p1];
NSArray *ary1 = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:t1 error:nil];
for (NSString *p1 in ary1)
{
NSString *t2 = [t1 stringByAppendingPathComponent:p1];
if([t2 rangeOfString:@".ttf"].location != NSNotFound)
{
[self registerFont:t2];
}
}
}
####2銆佸姩鎬佷笅杞借嫻鏋滄彁渚涚殑瀛椾綋
澶у鏁扮殑涓枃瀛椾綋鏄湁鐗堟潈鐨勶紝鍦ㄥ簲鐢ㄤ腑鍔犲叆鐗规畩涓枃瀛椾綋杩橀渶瑕佸鐞嗙浉搴旂殑鐗堟潈闂銆備粠iOS6寮�濮嬶紝鑻规灉灏辨敮鎸佸姩鎬佷笅杞戒腑鏂囧瓧浣撳埌绯荤粺涓��
锛�1锛夎嫻鏋滄敮鎸佷笅杞界殑瀛椾綋鍒楄〃
1锛塱OS6瀛椾綋鍒楄〃锛歨ttp://support.apple.com/zh-cn/HT202599
2锛塱OS7瀛椾綋鍒楄〃锛歨ttp://support.apple.com/zh-cn/HT5878
锛�2锛夊畼鏂规彁渚涚殑绀轰緥浠g爜
璁块棶https://developer.apple.com/library/ios/samplecode/DownloadFont/Introduction/Intro.html涓嬭浇绀轰緥绋嬪簭銆傞拡瀵圭ず渚嬬▼搴忕畝鍗曚粙缁嶅涓嬶細
1锛夊垽鏂瓧浣撴槸鍚﹀凡缁忚涓嬭浇杩�
UIFont* aFont = [UIFont fontWithName:fontName size:12.];
if (aFont && ([aFont.fontName compare:fontName] == NSOrderedSame || [aFont.familyName compare:fontName] == NSOrderedSame)) {
// 瀛椾綋宸茬粡琚姞杞借繃锛屽彲浠ョ洿鎺ヤ娇鐢�
return;
}
2锛変笅杞藉瓧浣�
鏍规嵁瀛椾綋鐨凱ostScript鍚嶇О鏋勫缓涓嬭浇瀛椾綋鎵�闇�鐨勫弬鏁帮細
//浣跨敤瀛椾綋鐨凱ostScript鍚嶇О鏋勫缓涓�涓瓧鍏�
NSMutableDictionary *attrs = [NSMutableDictionary dictionaryWithObjectsAndKeys:fontName, kCTFontNameAttribute, nil];
//鏍规嵁涓婇潰鐨勫瓧鍏稿垱寤轰竴涓瓧浣撴弿杩板璞�
CTFontDescriptorRef desc = CTFontDescriptorCreateWithAttributes((__bridge CFDictionaryRef)attrs);
//灏嗗瓧浣撴弿杩板璞℃斁鍒颁竴涓暟缁勪腑
NSMutableArray *descs = [NSMutableArray arrayWithCapacity:0];
[descs addObject:(__bridge id)desc];
CFRelease(desc);
涓嬭浇瀛椾綋鏂囦欢锛�
_block BOOL errorDuringDownload = NO;
CTFontDescriptorMatchFontDescriptorsWithProgressHandler( (__bridge CFArrayRef)descs, NULL,聽 ^(CTFontDescriptorMatchingState state, CFDictionaryRef progressParameter) {
聽 聽 //涓嬭浇鐨勮繘搴β� 聽
double progressValue = [[(__bridge NSDictionary *)progressParameter objectForKey:(id)kCTFontDescriptorMatchingPercentage] doubleValue];
if (state == kCTFontDescriptorMatchingDidBegin) {
dispatch_async( dispatch_get_main_queue(), ^ {
//寮�濮嬪尮閰�
NSLog(@"Begin Matching");
});
} else if (state == kCTFontDescriptorMatchingDidFinish) {
dispatch_async( dispatch_get_main_queue(), ^ {
if (!errorDuringDownload) {
//瀛椾綋涓嬭浇瀹屾垚
NSLog(@"%@ downloaded", fontName);
聽 聽 聽 聽 聽 聽 聽 //TODO:鍦ㄦ淇敼UI鎺т欢鐨勫瓧浣撴牱寮�
}
});
} else if (state == kCTFontDescriptorMatchingWillBeginDownloading) {
//寮�濮嬩笅杞�
聽 聽 聽 聽 NSLog(@"Begin Downloading");
聽 聽 聽 聽 dispatch_async( dispatch_get_main_queue(), ^ {
聽 聽 聽 聽 //TODO:鍦ㄦ鏄剧ず涓嬭浇杩涘害鎻愮ず
});
} else if (state == kCTFontDescriptorMatchingDidFinishDownloading) {
//涓嬭浇瀹屾垚
NSLog(@"Finish downloading");
dispatch_async( dispatch_get_main_queue(), ^ {
//TODO:鍦ㄦ淇敼UI鎺т欢鐨勫瓧浣撴牱寮忥紝闅愯棌涓嬭浇杩涘害鎻愮ず
});
} else if (state == kCTFontDescriptorMatchingDownloading) {
//姝e湪涓嬭浇
NSLog(@"Downloading %.0f%% complete", progressValue);
dispatch_async( dispatch_get_main_queue(), ^ {
//TODO:鍦ㄦ淇敼涓嬭浇杩涘害鏉$殑鏁板��
});
} else if (state == kCTFontDescriptorMatchingDidFailWithError) {
//涓嬭浇閬囧埌閿欒锛岃幏鍙栭敊璇俊鎭�
NSError *error = [(__bridge NSDictionary *)progressParameter objectForKey:(id)kCTFontDescriptorMatchingError];
NSLog(@"%@", [error localizedDescription]);
//璁剧疆涓嬭浇閿欒鏍囧織
errorDuringDownload = YES;
}
聽 聽 return (bool)YES;
});
锛�3锛夎鏄�
1锛変娇鐢ㄥ姩鎬佷笅杞戒腑鏂囧瓧浣撶殑API鍙互鍔ㄦ�佸湴鍚慽OS绯荤粺涓坊鍔犲瓧浣撴枃浠讹紝杩欎簺瀛椾綋鏂囦欢閮芥槸涓嬭浇鍒扮郴缁熺殑鐩綍涓紙鐩綍鏄�/private/var/mobile/Library/Assets/com_apple_MobileAsset_Font/锛夛紝鎵�浠ュ苟涓嶄細閫犳垚搴旂敤浣撶Н鐨勫鍔狅紝鑰屼笖鍙互鍦ㄥ涓簲鐢ㄤ腑鍏变韩銆�
2锛夊浣曡幏鍙栧瓧浣撶殑PostScript鍜孎ontName锛熷彲浠ラ�氳繃Mac绯荤粺鑷甫鐨勫瓧浣撳唽鏉ユ煡鐪嬨�傚叿浣撹鍙傝�冨墠闈㈢殑姝ラ銆�
###鍙傝�冭祫鏂�
1銆�銆婂姩鎬佷笅杞借嫻鏋滄彁渚涚殑澶氱涓枃瀛椾綋銆�
2銆�銆婂瓧浣撳姞杞戒笁绉嶆柟寮忋��
3銆�銆婂湪iOS绋嬪簭涓娇鐢ㄨ嚜瀹氫箟瀛椾綋銆�