简体   繁体   中英

UILabel set bold font with sans-serif

I am trying to set bold font for UILabel as follows :

lblActivities.font = [UIFont fontWithName:@"sans-serif-Bold" size:15.0];  

But this is not appearing as bold.
Any suggestions?

1-You need your font in .otf or .ttf copied to your project. For example in Supporting Files.

2 - You need to edit .plist file.

3 - Add "Fonts provided by application" key into your plist and in Item 0 copy the exact filename of the font you copied to your Supporting files WITH extension. For example: " JosefinSansStd-Light_0.otf "

4 - Make sure that the font you imported to your app is being packed into app itself. Do that by selecting your Target, then Build Phases, then Copy Bundle Resources. If you don't see your font in there, drag it from Supporting Files.

5 - Finally, you would like to list all your fonts when the app starts just to see useable name for your font. You will do that with this little piece of code:

NSArray *fontFamilies = [UIFont familyNames];
for (int i = 0; i < [fontFamilies count]; i++)
{
 NSString *fontFamily = [fontFamilies objectAtIndex:i];
 NSArray *fontNames = [UIFont fontNamesForFamilyName:[fontFamilies objectAtIndex:i]];
 NSLog (@"%@: %@", fontFamily, fontNames);
}

Search for your font in printed results, for example, I would search for "Josefin" and I would see that actual font name is "JosefinSansStd-Light" . After that you only need to use that font by:

UIFont *customFont = [UIFont fontWithName:@"JosefinSansStd-Light" size:20];

In iOS8 you add your fonts directly to the project and they are visible in the interface builder. Modify your code to account for this but programmatically setting font for iOS7 and selecting it in xCode6 interface builder. PS. Interface builder in xCode6 gives you the correct font name that you can copy-paste into the code below.

#define SYSTEM_VERSION_LESS_THAN(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedAscending)

if(SYSTEM_VERSION_LESS_THAN(@"8.0"))
   {
       UIFont *customFont = [UIFont fontWithName:@"OpenSans-Light" size:32];
       self.registerLabel.font = customFont;  
   }

Hope this helps, cheers.

For More Information check This Post

you have direct option in Xcode 1. select the label on view controller 2. on your right hand side panel select attribute inspector . 3. select font text-box link 4. a alert box appear select font dropdownlist 5 select custom from that list 6 set your style bold from style dropdown

try this may be help you. Make sure the font name is correct or available in your application.

[lblActivities setFont:[UIFont fontWithName:@"sans-serif-Bold" size:15.0]];

The Font which you describe is not available in apple library by Default you have to use custom font for this.

see Below link

Using custom font in a UIWebView

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM