1. Dynamically download the Chinese fonts provided by the system

  • In order to achieve better font effect, add the font pack problem in the application:
  1. Large font files cause a sharp increase in application size
  2. Chinese fonts are usually copyrighted
  • The API for dynamically downloading Chinese fonts can dynamically add font files to the iOS system. These font files are downloaded to the system directory, so the application volume does not increase. After downloading, the font files can be shared among all applications. The font file is provided by iOS system, which eliminates the problem of font copyright

  • You’ll need to use PostScript names to download fonts, so if you want to dynamically download fonts, you’ll need to use the Mac app Font Book to get PostScript names for the fonts. A screenshot of the PostScript name of the font from the Font Book is shown below

  • Apple provides a link to the dynamic download code Demo project here. Download this Demo project and learn how to use the corresponding API

- (BOOL)isFontDownloaded:(NSString *)fontName {UIFont* aFont = [UIFont fontWithName:fontName Size: 12.0];if (aFont && ([aFont.fontName compare:fontName] == NSOrderedSame 
               || [aFont.familyName compare:fontName] == NSOrderedSame)) {
        return YES;
    } else {
        returnNO; }} // 2. If the font has been downloaded, it can be used directly. // Create a Dictionary NSMutableDictionary *attrs = [NSMutableDictionary] with the PostScript name of the font dictionaryWithObjectsAndKeys:fontName, kCTFontNameAttribute, nil]; / / create a font description object CTFontDescriptorRef CTFontDescriptorRef desc = CTFontDescriptorCreateWithAttributes ((__bridge CFDictionaryRef)attrs); NSMutableArray *descs = [NSMutableArray arrayWithCapacity:0]; [descs addObject:(__bridge id)desc]; CFRelease(desc); __block BOOL errorDuringDownload = NO; __block BOOL errorDuringDownload = NO; CTFontDescriptorMatchFontDescriptorsWithProgressHandler((CFArrayRef)descs, NULL, ^bool(CTFontDescriptorMatchingState state, CFDictionaryRef _Nonnull progressParameter){ double progressValue = [[(__bridge NSDictionary *)progressParameter objectForKey:(id)kCTFontDescriptorMatchingPercentage] doubleValue];if (state == kCTFontDescriptorMatchingDidBegin) {
        NSLog(@"Font already matched");
    } else if (state == kCTFontDescriptorMatchingDidFinish) {    
        if(! errorDuringDownload) { NSLog(@"Font %@ download completed", fontName); }}else if (state == kCTFontDescriptorMatchingWillBeginDownloading) {
        NSLog(@"Fonts start to download");
    } else if (state == kCTFontDescriptorMatchingDidFinishDownloading) {
        NSLog(@"Font download completed"); dispatch_async( dispatch_get_main_queue(), Font = [UIFont fontWithName:fontName size:12]; ^ {self.label.font = [UIFont fontWithName:fontName size:12]; }); }else if (state == kCTFontDescriptorMatchingDownloading) {
        NSLog(@"Download progress %.0f%%", progressValue);
    } else if (state == kCTFontDescriptorMatchingDidFailWithError) {
        NSError *error = [(__bridge NSDictionary *)progressParameter objectForKey:(id)kCTFontDescriptorMatchingError];
        if(error ! = nil) { _errorMessage = [error description]; }else {
            _errorMessage = @"ERROR MESSAGE IS NOT AVAILABLE!"; } // Set the flag errorDuringDownload = YES; NSLog(@"Download error: %@", _errorMessage);
    }
    return YES;
});
Copy the code

2. Import the TTF font file and use the customized font

  • Download the font import project (note the font copyright issues above)
  • In the info.plist file, tell the system which font file you want to import
  • Set the font to the corresponding control (use the font name PostScript name, we explained how to get the PostScript name above)
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 100, 300, 400)];
label.text = @"Simple interface for downloading and using files compatible with Chinese and Chinese written information technology standards";
label.numberOfLines = 0;
UIFont *font = [UIFont fontWithName:@"FZLTXHK--GBK1-0" size:40];
[self.view addSubview:label];
Copy the code

Attached: my blog address