How to programmatically change the font style in objective-c

I would like to change the font style (e.g. bold, regular, light, slant) programmatically. I know that I can use IB, but I would like to change it using programmatically. It is necessary to be guided by this. Sorry if this is a stupid question.

For example, my code is as follows:

lblAge.font = [UIFont fontWithName:@"Helvetica" size:20]; 

I would like to add a style that is regular. How to do it?

+6
source share
5 answers

That should make you go

 offerTitle.font = [UIFont fontWithName:@"TimesNewRomanPS-ItalicMT" size:14.0f];//here offerTitle is the instance of `UILabel` 

Hope this helps :)

+16
source

Try this solution:

 myLabel.font = [UIFont boldSystemFontOfSize:16.0f]; myLabel.font = [UIFont italicSystemFontOfSize:16.0f]; 

For regular size:

 myLabel.font = [UIFont systemFontOfSize:16.0f]; 

Hope this helps you.

+4
source

For iOS 8.2 and above, there is + systemFontOfSize:weight: which allows you to specify weighted system fonts.

+2
source

Check out the UIFont API. Assign the generated font to the font property of the assigned object

 oneLabel.font=[UIFont fontWithXXX]; 
+1
source
 static NSString *_myCustomFontName; + (NSString *)myCustomFontName:(NSString*)fontName{ if ( !_myCustomFontName ){ NSArray *arr = [UIFont fontNamesForFamilyName:fontName]; // I know I only have one font in this family if ( [arr count] > 0 ) _myCustomFontName = arr[0]; } return _myCustomFontName; } 
0
source

All Articles