鍦╥OS寮�鍙戜腑浣跨敤鑷畾涔夊瓧浣�

鍦╥OS鐨勯」鐩紑鍙戜腑缁忓父閬囧埌闇�瑕佷娇鐢ㄤ竴浜涜嚜瀹氫箟鐨勫瓧浣撴枃浠讹紝姣斿浠垮畫_GB2312銆佹柟姝e皬鏍囧畫_GBK绛夈�備箣鍓嶆垜浠负浜嗕娇鐢ㄨ繖浜涜嚜瀹氫箟鐨勫瓧浣擄紝鍦ㄥ簲鐢ㄧ殑璧勬簮鍖呬腑鏀惧叆杩欎簺瀛椾綋鏂囦欢銆傚洜涓哄瓧浣撴枃浠堕�氬父姣旇緝澶э紝鏈夌殑涓�涓瓧搴撳氨杈惧埌10M浠ヤ笂锛堟嬁鏂规灏忔爣瀹媉GBK杩欎釜瀛楀簱鏉ヨ灏辨湁13M涔嬪锛夛紝杩欐牱鎵撳寘鍚庣殑ipa鏂囦欢鐨勪綋绉氨鍙兘浼氬彉寰楀緢澶э紝瀵逛簬鍙湁涓埆鐨勬ā鍧楅渶瑕佺壒娈婄殑瀛椾綋鏍峰紡鐨勫簲鐢ㄦ潵璇村緢涓嶅垝绠楋紝閭d箞鍦╥OS6.0浠ュ悗鑻规灉灏卞紑鏀句簡鍔ㄦ�佸姞杞藉瓧浣撶殑鏉冮檺銆備笅闈㈠氨iOS涓娇鐢ㄥ瓧浣撶殑杩欎袱绉嶆柟寮忚繘琛屼粙缁嶃��

###浣跨敤闈欐�佸瓧浣�

1銆佸皢瀛椾綋鏂囦欢鎷疯礉鍒伴」鐩伐绋嬩腑锛屽湪Info.plist鏂囦欢涓坊鍔燜onts provided by application鐨勯厤缃」锛屽叾涓瘡涓�涓狪tem瀵瑰簲鐨勬槸瀛椾綋鏂囦欢鐨勫悕绉帮紝濡侱S-DIGI.TTF銆�

鍦╥OS寮�鍙戜腑浣跨敤鑷畾涔夊瓧浣�_第1张图片

2銆佷娇鐢ㄦ椂鐩存帴鎸夌収濡備笅鏂瑰紡鍗冲彲锛�

_textLabel1.font = [UIFont fontWithName:@"DS-Digital" size:40];

鏁堟灉濡備笅锛�

鍦╥OS寮�鍙戜腑浣跨敤鑷畾涔夊瓧浣�_第2张图片

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鑷甫鐨勫瓧浣撳唽鏌ョ湅瀛椾綋鐨勫悕绉�

鐩存帴鍙屽嚮瀛椾綋鍗冲彲鎵撳紑瀛椾綋鍐岋紝濡傛灉绯荤粺娌℃湁瀹夎璇ュ瓧浣撴寜鐓ц姹傚畨瑁呭嵆鍙紝鐒跺悗鍙互鍦ㄥ瓧浣撶殑璇︾粏淇℃伅涓壘鍒板搴旂殑瀛椾綋鐨勫悕绉帮細

鍦╥OS寮�鍙戜腑浣跨敤鑷畾涔夊瓧浣�_第3张图片

###浣跨敤鍔ㄦ�佸瓧浣�

####1銆佸姩鎬佷笅杞借嚜瀹氫箟鐨勫瓧浣�

鍦ㄧ綉鏄撴柊闂籭OS瀹㈡埛绔腑鍙互浣跨敤鑷畾涔夌殑瀛椾綋锛屽浜庢湭涓嬭浇鐨勫瓧浣撳彲鍏堜笅杞界劧鍚庡畨瑁呬笅娆″氨鑳借嚜鍔ㄨ缃负璇ュ瓧浣擄紝鏁堟灉濡備笅锛�

鍦╥OS寮�鍙戜腑浣跨敤鑷畾涔夊瓧浣�_第4张图片

涓嬮潰灏辫鍔熻兘绠�鍗曚粙缁嶅疄鐜扮殑姝ラ

#####锛�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 锛孋oreText妗嗘灦銆�

#####锛�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绋嬪簭涓娇鐢ㄨ嚜瀹氫箟瀛椾綋銆�

你可能感兴趣的:(鍦╥OS寮�鍙戜腑浣跨敤鑷畾涔夊瓧浣�)