. Using fonts provided by the system default
The default font provided by the system is primarily the font provided in Uifont, which uses the following code:
Fontlabel.font = [Uifont fontwithname:@ "Marion" size:17];
or set the font properties through a font-specific dictionary
/* uifontdescriptorfamilyattribute: Set font family name uifontdescriptornameattribute : Set font name for fonts Uifontdescriptorsizeattribute : Set Font size uifontdescriptormatrixattribute: Set font deformation * / Uifontdescriptor *attributefontdescriptor = [uifontdescriptor fontdescriptorwithfontattributes: @{ Uifontdescriptorfamilyattribute: @ "Marion", uifontdescriptornameattribute:@ "Marion-regular", Uifontdescriptorsizeattribute: @40.0, uifontdescriptormatrixattribute:[nsvalue valuewithcgaffinetransform: Cgaffinetransformmakerotation (m_1_pi*1.5) ]}]; Fnotlabel.font = [Uifont fontwithdescriptor:attributefontdescriptor size:0.0];
The font family name and font name can be obtained in the following ways
NSLog (@ "familynames:%@", [Uifont familynames]);
Both of these methods can be used to set the font for the label, but all are only for English numerals and are not valid for Chinese. There are two other ways to change the Chinese font to use the following
2. Dynamically download fonts
IOS6 after the Apple began to support the dynamic download of Chinese fonts have been used in the application to display the demand for personalized fonts, because the download needs to use the name is the PostScript name, you need to use the Mac's own app "font book" to get the PostScript name of the corresponding font. The following shows the PostScript name of the doll body-complex font from the font book
The specific code will not be introduced, you can refer to the relevant documents provided by Apple: https://developer.apple.com/library/ios/samplecode/DownloadFont/Listings/ Downloadfont_viewcontroller_m.html#//apple_ref/doc/uid/dts40013404-downloadfont_viewcontroller_ M-dontlinkelementid_6
Or you can refer to Mr. Tang Qi's blog For more detailed introduction: http://blog.devtang.com/blog/2013/08/11/ios-asian-font-download-introduction/
Here is my research after the test demo, to provide you with reference:
-(void) Asynchronouslysetfontname: (NSString *) fontname{uifont* afont = [Uifont fontwithname:fontname size:24]; If the font is already downloaded if (Afont && ([afont.fontname compare:fontname] = = Nsorderedsame | | [Afont.familyname compare:fontname] = = Nsorderedsame) {//Go ahead and display the sample text. _flabelview.text = @ "Welcome to my Blog"; _flabelview.font = [Uifont fontwithname:fontname size:24]; Return }//Create a dictionary with the font s PostScript name. Nsmutabledictionary *attrs = [Nsmutabledictionary dictionarywithobjectsandkeys:fontname, KCTFontNameAttribute, nil]; Create a new font descriptor reference from the attributes dictionary. Ctfontdescriptorref desc = ctfontdescriptorcreatewithattributes ((__bridge cfdictionaryref) attrs); Nsmutablearray *descs = [Nsmutablearray arraywithcapacity:0]; [Descs AddObject: (__bridge ID) desc]; Cfrelease (DESC); __block BOOL errorduringdownload =NO; Start processing the font descriptor. This function returns the immediately, but can potentially take a long time to process. The progress is notified via the callback block of Ctfontdescriptorprogresshandler type. See CTFontDescriptor.h for the list of progress states and keys for Progressparameter dictionary. Ctfontdescriptormatchfontdescriptorswithprogresshandler ((__bridge cfarrayref) Descs, NULL, ^ ( Ctfontdescriptormatchingstate state, Cfdictionaryref progressparameter) {//nslog (@ ' state%d-%@ ', state, Progressparameter); Double Progressvalue = [[(__bridge nsdictionary *) Progressparameter Objectforkey: (ID) Kctfontdescriptormatchingpercentage] Doublevalue]; if (state = = Kctfontdescriptormatchingdidbegin) {Dispatch_async (Dispatch_get_main_queue (), ^ { Show an Activity indicator NSLog (@ "Begin Matching"); }); } else if (state = = KctfontdescriptormAtchingdidfinish) {Dispatch_async (Dispatch_get_main_queue (), ^ {//Remove the activity indica Tor//Display The sample text for the newly downloaded font _flabelview.tex T = @ "Welcome to my Blog"; _flabelview.font = [Uifont fontwithname:fontname size:24]; Log the font URL in the console ctfontref fontref = Ctfontcreatewithname ((__bridge cfstringref) FontName, 0., NULL); Cfstringref Fonturl = Ctfontcopyattribute (Fontref, Kctfonturlattribute); NSLog (@ "%@", (__bridge nsurl*) (Fonturl)); Cfrelease (Fonturl); Cfrelease (FONTREF); if (!errorduringdownload) {NSLog (@ "%@ downloaded", fontname); } }); } else if (state = = kctfontdescriptormatchingwillbegindownloading) {Dispatch_async (Dispatch_get_main_queue (), ^ { Show a progress bar NSLog (@ "Begin downloading"); }); } else if (state = = kctfontdescriptormatchingdidfinishdownloading) {Dispatch_async (Dispatch_get_main_queue (), ^ {//Remove the progress bar NSLog (@ "Finish downloading"); }); } else if (state = = kctfontdescriptormatchingdownloading) {Dispatch_async (Dispatch_get_main_queue (), ^ { Use the progress bar to indicate the progress of the downloading NSLog (@ "Downloading%.0f%% C Omplete ", progressvalue); }); } else if (state = = Kctfontdescriptormatchingdidfailwitherror) {//An error has occurred. Get the error message Nserror *error = [(__bridge nsdictionary *) Progressparameter Objectforkey: (ID) kctfontd Escriptormatchingerror]; if (Error! = nil) {_errormessage = [error description]; } else{_errormessage = @ "ERROR MESSAGE is not available!";} Set our flag errorduringdownload = YES; Dispatch_async (Dispatch_get_main_queue (), ^ {NSLog (@ "Download error:%@", _errormessage); }); } return (bool) YES; }); }
Just call it in the appropriate place:
-(void) viewdidload { [super viewdidload]; Additional setup after loading the view, typically from a nib. _flabelview = [[Uitextview alloc] Initwithframe:cgrectmake ((+), [[]]]; [Self.view Addsubview:_flabelview]; [Self asynchronouslysetfontname:@ "hanzipensc-w3"]; }
Here is the result after the run:
3. Introduction of external fonts
Now, whether it's a Windows font or an Android font, as long as it's in TTF format, or the TTC, OTF format provided by Apple, the iOS program supports inline. Specific practices:
Drag the font you want to download to your project first
Add a field to the info file
You can then use the method provided above [Uifont fontwithname:@ "mini-Mimi" Size:17] method to set this font in English, digital, or Chinese. can output [Uifont Familynames] detection has been added
You can also set this font for a label in Xib.
Download fonts on the web is not necessarily all can be used, the following provides some common fonts for everyone to download:
Link: Http://pan.baidu.com/s/1kTVX8qF Password: vdwa
To get a more comprehensive font, you can also use the various font formats provided by Apple itself, or it can be obtained using the "font side" of the Mac app, for example:
Usage is the same as the downloaded font
Ios-font (EXT)