简体   繁体   中英

How do I set bold font on UILabel in iOS?

I want to set bold font on UILabel . I generate label by coding. when i try to set bold font on label ios set system regular font but i want to set 'Noto sans-Bold' font. In storyboard set font completely.

My code is

UILabel *lblName=[[UILabel alloc]initWithFrame:CGRectMake(0, 25, 100, 21)];
lblName.text=@"Hello World";
[lblName setFont:[UIFont fontWithName:@"Noto Sans-Bold" size:15.0]];
[self.view addSubview:lblName];

If you haven't got the formatting of your font title correct, it will automatically default back to system font.

The issue is with your font name and since Noto isn't native it's going to be difficult to know exactly what name you need to provide.

Make sure you have added that font to your project.

You can check your available fonts by running this code:

SWIFT

for familyName in UIFont.familyNames() {
    print("\n-- \(familyName) \n")
    for fontName in UIFont.fontNamesForFamilyName(familyName) {
        print(fontName)
    }
}

OBJECTIVE-C

for (NSString *familyName in [UIFont familyNames]){
    NSLog(@"Family name: %@", familyName);
    for (NSString *fontName in [UIFont fontNamesForFamilyName:familyName]) {
        NSLog(@"--Font name: %@", fontName);
    }
}

lblname.font = [UIFont fontWithName:@"Noto Sans-Bold" size:18];

and one more thing check your font correctly it is available in list or not.

Here is a list of available fonts .

List of available fonts

EDIT:- or if you want to set font which not available in xcode then you have to first install it in XCode please follow this tutorial.

Install custom fonts

As said by Jilouc follow this procedure.

  1. Add the font files to your resource files
  2. Edit your Info.plist : Add a new entry with the key Fonts provided by application .
  3. For each of your files, add the file name to this array

On the example below, I've added the font "DejaVu Sans Mono":

在此处输入图片说明 In your application you can the use [UIFont fontWithName:@"DejaVuSansMono-Bold" size:14.f] .

Or if you want to use it in html/css, just use font-family:DejaVu Sans Mono;

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