2009-08-19 128 views
28

我的应用程序中有以下代码。UILabel - 以编程方式在iPhone中设置字体 - 字体

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

现在,我需要知道,

======>如何通过代码集 “字样=大胆”?

+0

“标签的阴影偏移”是指标签阴影的大小吗? – devinfoley 2009-08-19 21:59:35

+0

@flashcards - 我得到的解决方案不需要找出影子。 [tmp setshadowoffset:cgsizemake(1,1)]; – 2009-08-19 22:13:41

+2

@sagar您应该将其作为您自己问题的答案发布,以便保存。干杯! – devinfoley 2009-08-19 22:20:00

回答

36

您将需要使用家族内的bold字体的名称。要了解是否有bold版本的美国打字机,尝试输出

[UIFont fontNamesForFamilyName:@"AmericanTypewriter"] 

到控制台。

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

[UIFont fontNamesForFamilyName:@"AmericanTypewriter-Bold"] 
+0

@flashcards - 不,我试过你的逻辑,但我的应用程序崩溃了。 – 2009-08-19 22:07:03

+0

嗯......看起来像fontNamesForFamilyName是UIFont上的一个静态方法,它带有一个姓氏。我现在会解决我的问题。你尝试使用“AmericanTypewriter-Bold”吗? – devinfoley 2009-08-19 22:17:49

+5

@sagar - “美国打字机”之间没有空格。我认为你已经在他们之间放置了空间。 – Brij 2009-12-11 13:20:32

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

这只有在你想使用系统字体时才有用。 @Spark没有使用系统字体。 – 2012-10-03 12:29:20

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

像@devinfoley写道,你必须添加-BoldfontName你使用。对我来说,它不适用于fontNamesForFamilyName,而是我使用fontWithName:size。如果以编程方式创建UILabel,可能会有效。在我的情况下,我只是将font设置在我的UILabel的扩展名内awakeFromNib中,并将此扩展名设置为Identity Inspector中的class,用于我的特定UILabel。使用self.font.pointSize,您可以在Interface Builder内设置fontSize,如果您有更多的UI元素,可以更好地处理它。

- (void)awakeFromNib{ 

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

如果您font不工作,你应该打印所有的家庭fonts,所以你可以看到,如果你写的fontName错误。这样你也可以检查是否有bold字体可用,如果没有打印你没有这个选项。

NSLog (@"Available Fonts: %@", [UIFont fontNamesForFamilyName:@"Your Font"]); 

更多font东西:https://stackoverflow.com/a/8529661/1141395

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

斯威夫特更新

提到如果您已经添加的字体应用程序(如解释here ),可以使用雨燕的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) 
相关问题