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”?

4

6 回答 6

36

您将需要使用该bold系列中的字体名称。要了解是否有boldAmerican Typewriter 的版本,请尝试输出

[UIFont fontNamesForFamilyName:@"AmericanTypewriter"] 

到控制台。

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

[UIFont fontNamesForFamilyName:@"AmericanTypewriter-Bold"] 
于 2009-08-19T21:58:32.717 回答
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];
于 2011-09-03T05:18:59.143 回答
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];
于 2012-11-14T20:03:18.863 回答
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];
于 2016-02-12T10:27:37.367 回答
1

就像@devinfoley 写的那样,您必须添加-BoldfontName您正在使用的内容中。对我来说,它不适用于fontNamesForFamilyName,而是我正在使用fontWithName:size. 如果您以UILabel编程方式创建它,它可能会起作用。在我的情况下,我只是设置我的扩展名的font内部,并将这个扩展名设置为我UILabel的特定. 如果您有更多的 UI 元素,您可以设置内部并且可以更好地处理它。awakeFromNibclassIdentity InspectorUILabelself.font.pointSizefontSizeInterface Builder

- (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

于 2014-01-07T10:46:48.810 回答
0

快速更新

如果您已经将字体添加到应用程序(如此所述),您可以使用 Swift 的extension功能,例如 aUILabel和 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)
于 2016-12-05T20:57:06.607 回答