Three methods for loading iOS fonts and three methods for loading ios Fonts

Source: Internet
Author: User

Three methods for loading iOS fonts and three methods for loading ios Fonts

  • Static Loading
  • Dynamic Loading
  • Dynamic download of various fonts provided by Apple
  • Others
    • Print all available fonts
    • Check whether a font has been downloaded

This is a short article that describes three methods for loading iOS custom fonts.

Static Loading

This is the simplest and most intuitive way to load custom fonts. As long as the font is included in the project, modify the plist file of the project, add the Fonts provided by application field, and write the font file name to this array.

And then use it directly.

- (void)staticLoad{    self.staticFontLabel.font = [UIFont fontWithName:@"MFYingHua_Noncommercial-Regular" size:50];}
Dynamic Loading

Sometimes we do not want to include the font in the bundle for static loading. For example, if the font provider encrypts the font, it must be decrypted when the program is running. Or you want to control the font of the App through the backend. Then we need to dynamically load the font.

-(Void) dynamicLoad {// path of the font file NSString * URL_FONT = @ "http: // 192.168.1.12: 8888/static/MFDingDing. otf "; // NSString * fontName = @" MFDingDing_Noncommercial-Regular "; // download the font NSData * dynamicFontData = [NSData dataWithContentsOfURL: [NSURL URLWithString: URL_FONT]; if (! DynamicFontData) return; CFErrorRef error; CGDataProviderRef providerRef = CGDataProviderCreateWithCFData (CFDataRef) dynamicFontData); CGFontRef font = require (providerRef); if (! CTFontManagerRegisterGraphicsFont (font, & error) {// if the registration fails, CFStringRef errorDescription = CFErrorCopyDescription (error); NSLog (@ "Failed to load font: % @", errorDescription); CFRelease (errorDescription);} else self. dynamicFontLabel. font = [UIFont fontWithName: fontName size: 50]; CFRelease (font); CFRelease (providerRef );}

Note: You need to add CoreText. framework and include the header file # import <CoreText/CoreText. h>.

Dynamic download of various fonts provided by Apple

Since iOS 6, Apple has provided a new API that allows us to dynamically download additional fonts provided by Apple. In addition, these fonts are downloaded to the system. That is to say, if other apps use this font or the App deletes the font and reinstalls it, you do not need to download the font any more.

Font list

Here we recommend a blog post from Tang Qiao: Dynamic download of various Chinese fonts provided by Apple. The author introduces this function in detail. So here we will not repeat it.

Others

Finally, two small functions are provided.

Print all available fonts
- (void)printAllFonts{    NSArray *fontFamilies = [UIFont familyNames];    for (NSString *fontFamily in fontFamilies)    {        NSArray *fontNames = [UIFont fontNamesForFamilyName:fontFamily];        NSLog (@"%@: %@", fontFamily, fontNames);    }}
Check whether a font has been downloaded
- (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;}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.