UILabel - 在iPhone中以编程方式设置字体和字形。

28

我在我的应用程序中有以下代码。

tmp=[[UILabel alloc] initWithFrame:label1Frame];
tmp.tag=1;
tmp.textColor=[UIColor blackColor];
[tmp setFont:[UIFont fontWithName:@"American Typewriter" size:18]];
tmp.backgroundColor=[UIColor clearColor];
[cell.contentView addSubview:tmp];
[tmp release];

现在,我需要知道如何通过代码设置"Typeface=bold"?


“标签的阴影偏移”是指标签的投影大小吗? - devinfoley
@闪卡 - 我已经找到了解决方案,不需要再寻找阴影的问题。 [tmp setshadowoffset:cgsizemake(1,1)]; - sagarkothari
2
@sagar 你应该将这个作为答案发布在你自己的问题中,这样它就会被保存。干杯! - devinfoley
6个回答

36

你需要使用字族中粗体字体的名称。要查找是否有American Typewriter的粗体版本,请尝试输出

[UIFont fontNamesForFamilyName:@"AmericanTypewriter"] 

将内容输出到控制台。

在这种情况下,你应该使用 "AmericanTypewriter-Bold"

[UIFont fontNamesForFamilyName:@"AmericanTypewriter-Bold"] 

@flashcards - 不好意思,我尝试了你的逻辑,但是我的应用程序崩溃了。 - sagarkothari
嗯...看起来fontNamesForFamilyName是UIFont上的一个静态方法,需要输入一个字体族名。我现在会修正我的答案。你试过使用"AmericanTypewriter-Bold"吗? - devinfoley
5
@sagar - "American Typewriter"中间不应该有空格。我觉得你在它们之间放了一个空格。 - Brij
1
确实,我刚试了一下,应该是[UIFont fontWithName:@"AmericanTypewriter-Bold" size:18.f] - DaGaMs
我发现如果我错误地指定了字体名称,我会收到一个错误。然而iOS只是默认使用标准字体。可以在这里获取字体名称和字形的列表(https://gist.github.com/shannoga/1008678)。 - PhillipOReilly

20

使用setter属性如何?

// Create a string  
NSString *text = @"You are getting sleepy.";

// Get a font to draw it in  
UIFont *font = [UIFont boldSystemFontOfSize:28];

// Draw the string  
[text drawInRect:(CGRect)
withFont:font];

这只有在您想使用系统字体时才有用。@Spark 没有使用系统字体。 - Josh Paradroid

17

只需使用NSLog来获取您想要的字体:

NSLog(@" %@", [UIFont fontNamesForFamilyName:@"American Typewriter"]);

然后你会得到这个数组:

(
    "AmericanTypewriter-CondensedLight",
    "AmericanTypewriter-Light",
    "AmericanTypewriter-Bold",
    AmericanTypewriter,
    "AmericanTypewriter-CondensedBold",
    "AmericanTypewriter-Condensed"
)

使用任何你在代码中需要的东西:

UILabel * loading = [[UILabel alloc] initWithFrame:CGRectMake(350, 402, 150, 25)];
    [loading setFont:[UIFont fontWithName:@"AmericanTypewriter-Bold" size:12.0]];
    [loading setTextColor:[UIColor whiteColor]];
    [loading setBackgroundColor:[UIColor clearColor]];
    [loading setText:@"Loading ..."];
    [self.view addSubview:loading];

2
你可以在这个网站iOSfonts找到所有支持iOS的字体名称。 预览你想要的准确字符串,找到最好的字体并按照上面的答案使用它。
UILabel * myLabel = [[UILabel alloc] initWithFrame:CGRectMake(100, 400, 150, 50)];
    [loading setFont:[UIFont fontWithName:@"Avenir-Black" size:12.0]];
    [loading setTextColor:[UIColor redColor]];
    [loading setBackgroundColor:[UIColor clearColor]];
    [loading setText:@"My Label Title"];
    [self.view myLabel];

1

就像@devinfoley所写的那样,您必须将-Bold添加到正在使用的fontName中。对于我来说,使用fontNamesForFamilyName无法正常工作,而是使用fontWithName:size。也许如果您以编程方式创建UILabel,则会起作用。在我的情况下,我只是在我的UILabel的扩展内设置字体,并在awakeFromNib中将此扩展设置为class,并针对我的特定UILabel在Identity Inspector中进行设置。使用self.font.pointSize,您可以在Interface Builder中设置fontSize,并且如果您有更多的UI元素,可以更好地处理它。

- (void)awakeFromNib{

    [super awakeFromNib];
    [self setAdjustsFontSizeToFitWidth:YES];
    [self setFont:[UIFont fontWithName:@"Font-Bold" size:self.font.pointSize]];
}

如果你的字体不能正常工作,你应该打印出该字体家族的所有字体,这样你就可以看到是否错误地输入了fontName。这样你也可以检查是否有粗体字体可用,如果没有打印出来,那么你就没有这个选项。
NSLog (@"Available Fonts: %@", [UIFont fontNamesForFamilyName:@"Your Font"]);

更多字体相关内容:https://dev59.com/2moy5IYBdhLWcg3wdt4L#8529661


0

Swift 更新

如果您已经将字体添加到应用程序中(如此处所述),则可以使用 Swift 的 extension 功能,例如,使用 UILabel 和 Roboto 字体:

extension UILabel {

    func setFont(style: String, size: Int){
        self.font = UIFont(name: style, size: CGFloat(size))
    }

    struct FontStyle {
        public static let italic: String = "Roboto-LightItalic"
        public static let normal: String = "Roboto-Light"
        public static let thin: String = "Roboto-Thin"
    }
}

Roboto-LightItalicRoboto-Light 是 TTF 字体文件的名称。然后,你可以简单地调用它们。

someUILabel.setFont(style: UILabel.FontStyle.normal, size: 20)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接