IOS user-defined font (built-in and arbitrary download of ttf \ otf \ ttc font files)
Recently I made an application on reading, used custom fonts, and learned this knowledge. 1. the first is the simplest and most common practice. Package the built-in character library file: add the library file to the project, such as font1.ttf, and then add a Fonts provided by application to the project plist, this is an array, and then add key item1. value is the font1.ttf just mentioned. You can directly use this font in the project and use + (UIFont *) fontWithName :( NSString *) fontName size: (CGFloat) fontSize. However, it should be noted that this fontName is not a file name, but a real fontName in it. Above font1.ttf inside the font is MFQingShu_Noncommercial-Regular, then directly with UIFont * font = [UIFont fontWithName: @ "MFQingShu_Noncommercial-Regular" size: 12]; can go to the correct font. 2. But in general, the font files are relatively large and should not be built in. If we use plist pre-defined methods, we certainly cannot cover all of them. As a result, users cannot use more fonts they like. So we should use the code to read the font: Provide the font file path, and return the required Font: copy the code-(UIFont *) mfmfontwithpath :( NSString *) path size :( CGFloat) size {NSURL * fontUrl = [NSURL fileURLWithPath: path]; CGDataProviderRef forward = forward (_ bridge CFURLRef) fontUrl); CGFontRef fontRef = forward (forward); Forward (forward ); CTFontManagerRegisterGraphicsFont (fontRef, NULL); NSString * fontName = CFBridgingRelease (Response (fontRef); UIFont * font = [UIFont fontWithName: fontName size: size]; CGFontRelease (fontRef); return font ;} copy the code so that you do not need to set anything in plist. You only need to get the path of the library file to retrieve the corresponding font. The above method is valid for TTF and OTF fonts, but only one type of fonts is available for TTC fonts. Because the TTC font is a collection of similar fonts, it is generally a combination of fonts. Therefore, if the font requirements are relatively high, you can use the following method to retrieve all fonts: copy the code-(NSArray *) mfmfontarraywithpath :( NSString *) path size :( CGFloat) size {CFStringRef fontPath = CFStringCreateWithCString (NULL, [path UTF8String], signature); CFURLRef fontUrl = Signature (NULL, fontPath, signature, 0); CFArrayRef fontArray = Signature (fontUrl); expires RL (fontUrl, kCTFontManagerScopeNone, NULL); NSMutableArray * customFontArray = [NSMutableArray array]; for (CFIndex I = 0; I <CFArrayGetCount (fontArray); I ++) {CTFontDescriptorRef descriptor = Response (fontArray, I); CTFontRef fontRef = Response (descriptor, size, NULL); NSString * fontName = CFBridgingRelease (CTFontCopyName (fontRef, callback); U IFont * font = [UIFont fontWithName: fontName size: size]; [customFontArray addObject: font];} return customFontArray;} copy the Code. However, this method only supports more than 7.0, currently, no method is found below 7.0. In my opinion, because the fonts in the ttc are similar, it is enough to use one. Appendix: (font introduction) TTF (TrueTypeFont) is a font name. TTF (TrueTypeFont) is a font file format jointly launched by Apple and Microsoft. With the popularity of windows, it has become the most commonly used font file representation. The TTC font is a TrueType font integrated file (. A separate file structure contains multiple fonts to more effectively share the profile data. When multiple fonts share the same stroke, the TTC technology can effectively reduce the size of small font files. TTC is a combination of several TTF fonts. After installation, more than two fonts are displayed in the font list. When most of the two fonts are the same, you can make the two fonts into a TTC file. The common TTC fonts are shared with stroke data, therefore, most of the differences in font in this set are only the differences in character width to adapt to different layout requirements.