Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the ways iOS sets fonts?

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces "what are the ways of setting fonts by iOS". In daily operation, I believe that many people have doubts about the way iOS sets fonts. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the doubts of "what is the way of setting fonts by iOS?" Next, please follow the editor to study!

Sometimes the project needs to display some non-system fonts to achieve some UI effect, currently there are three ways to set fonts, default mode, bundle mode, coreText mode.

1 default mode

This is the normal font setting method.

Label.font = [UIFont fontwithname:@ "Blazed" size:42]

As for the name of the first parameter, you can output a list of all font names by

[UIFont familyNames]

Anything that exists in the list of names can be associated to the corresponding font in this way.

2 bind a custom font package

In fact, the second way is the same as the first way in the code, still setting the font by name.

Label.font = [UIFont fontwithname:@ "Blazed" size:42]

Just to use the downloaded font file, first add the font file to the font directory of the system.

The specific operations are as follows:

1 download the font file of the target and add it to the project, named fontTest.ttf

2 in info.plist, add Fonts provided by application entry, open the corresponding array, you can set multiple fonts, enter fontTest.ttf in Item0.

At this time, execute [UIFont familyNames], and the newly added font exists in the list, so you can set the font directly by setting the name.

3 bind fonts through CoreText

The second way can basically meet most of the needs, but there is only one problem, that is, the font package size is not certain, ranging from tens of megabytes to hundreds of megabytes. As other resource packages enter ipa together, the package will become very large, especially if some businesses need a lot of font packages, then they must need to be downloaded from the dynamic network. This dynamically downloaded font file currently has no way to dynamically add configuration information to info.plist through Apple's currently open interface. At this point, you need to dynamically bind the CoreText interface to memory.

Of course, the CoreText framework needs to be introduced here.

# import

< CoreText/CoreText.h>

+ (UIFont*) doGetCustomFontWithPath: (NSString*) path size: (CGFloat) size {if (! path) {return [UIFont systemFontOfSize:21];} / / NSString* path3 = [path stringByAppendingString:@ "1"]; NSURL * fontUrl = [NSURL fileURLWithPath:path]; / / [NSFileManager defaultManager] copyItemAtPath:path toPath:path3 error:nil]; CGDataProviderRef fontDataProvider = CGDataProviderCreateWithURL ((_ _ bridge CFURLRef) fontUrl); CGFontRef fontRef = CGFontCreateWithDataProvider (fontDataProvider); CGDataProviderRelease (fontDataProvider); CTFontManagerRegisterGraphicsFont (fontRef, NULL); NSString* fontName = CFBridgingRelease (CGFontCopyPostScriptName (fontRef)) UIFont * font = [UIFont fontWithName:fontName size:size]; CGFontRelease (fontRef); return font;}

At this point, the study of "what is the way iOS sets fonts" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report