如何在 iOS 中的 UILabel 上设置粗体?

How do I set bold font on UILabel in iOS?

我想在 UILabel 上设置粗体。我通过编码生成标签。当我尝试在标签 ios 上设置粗体字体时设置系统常规字体但我想设置 'Noto sans-Bold' 字体。在情节提要中完全设置字体。

我的密码是

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];

如果您的字体标题格式不正确,它会自动默认回系统字体。

问题出在您的字体名称上,由于 Noto 不是本地字体,因此很难确切知道您需要提供什么名称。

确保您已将该字体添加到您的项目中。

您可以通过运行此代码查看您可用的字体:

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];

还有一件事要检查你的字体是否在列表中是否可用。

这是可用字体的列表。

List of available fonts

编辑:- 或者如果你想设置 xcode 中不可用的字体,那么你必须先在 XCode 中安装它 请按照本教程进行操作。

Install custom fonts

Jilouc所述,请遵循此程序。

  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;