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

Sag*_*ari 28 iphone uilabel

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

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];
Run Code Online (Sandbox Code Playgroud)

现在,我需要知道,

======>如何通过代码设置"Typeface = bold"?

dev*_*ley 36

您将需要使用该系列中的bold字体名称.要查看是否有bold美国打字机版本,请尝试输出

[UIFont fontNamesForFamilyName:@"AmericanTypewriter"] 
Run Code Online (Sandbox Code Playgroud)

到控制台.

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

[UIFont fontNamesForFamilyName:@"AmericanTypewriter-Bold"] 
Run Code Online (Sandbox Code Playgroud)

  • @sagar - "美国打字机"之间没有空格.我想你已经在它们之间留出了空间. (5认同)

dcr*_*tar 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];
Run Code Online (Sandbox Code Playgroud)


Naz*_*zir 17

只是NSLog你想要的字体:

NSLog(@" %@", [UIFont fontNamesForFamilyName:@"American Typewriter"]);
Run Code Online (Sandbox Code Playgroud)

你得到了阵列:

(
    "AmericanTypewriter-CondensedLight",
    "AmericanTypewriter-Light",
    "AmericanTypewriter-Bold",
    AmericanTypewriter,
    "AmericanTypewriter-CondensedBold",
    "AmericanTypewriter-Condensed"
)
Run Code Online (Sandbox Code Playgroud)

在Code中使用您需要的任何内容:

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];
Run Code Online (Sandbox Code Playgroud)