UILabel - 设置自定义字体

时间:2011-11-16 11:56:09

标签: ios uilabel

我想将GillSans-Bold字体添加到UILabel。 我已经在xib文件中设置了它,我也在我的课堂中进行设置,如下所示:

[label setFont:[UIFont fontWithName:@"GillSans-Bold" size:18]];

但是,它似乎对我不起作用。 有什么建议吗?

4 个答案:

答案 0 :(得分:12)

iPhone 4.3没有Gill Sans,但iPad自3.2.1开始就拥有它。

list comparing fonts for iPad 4.3 and iPhone 4.3。可以肯定的是,这就是您获取设备上可用字体列表的方式:

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

如果说

GillSans
GillSans-Bold
GillSans-BoldItalic
GillSans-Italic

然后[UIFont fontWithName:@"GillSans-Bold" size:18]应返回有效字体。

答案 1 :(得分:3)

为了实现这一点,我必须在项目目录中添加此字体,并在Info.Plist文件中添加此字体

答案 2 :(得分:2)

字体GillSans-Bold是否存在?检查[UIFont fontWithName:@"GillSans-Bold" size:18]是否返回UIFont,而不是null

答案 3 :(得分:0)

迅捷

let label = UILabel()
    label.font = UIFont(name: "Roboto-Regular", size: 16)
    label.text = "Your text here"

附言在此之前,您必须:

  1. 在项目中添加自定义字体

enter image description here

  1. 在 Info.plist 文件中添加字体名称

enter image description here

  1. 在复制包资源中添加字体文件

enter image description here

之后你也可以在故事板中使用这种字体

enter image description here

相关问题