【问题标题】:UILabel - setting font - typeface programmatically in iPhoneUILabel - 在 iPhone 中以编程方式设置字体 - 字体
【发布时间】:2010-11-21 03:07:36
【问题描述】:

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

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

【问题讨论】:

  • “标签的阴影偏移量”是指标签阴影的大小吗?
  • @flashcards - 我得到了解决方案,无需寻找影子。 [tmp setshadowoffset:cgsizemake(1,1)];
  • @sagar 您应该将其发布为您自己问题的答案,以便保存。干杯!

标签: iphone uilabel


【解决方案1】:

Swift 更新

如果您已经将字体添加到应用程序(如 here 所述),您可以使用 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)

【讨论】:

    【解决方案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];
    

    【讨论】:

      【解决方案3】:

      就像@devinfoley 所写,您必须将-Bold 添加到您正在使用的fontName。对我来说,它不适用于fontNamesForFamilyName,而是我使用fontWithName:size。如果您以编程方式创建UILabel,也许它会起作用。在我的情况下,我只是在awakeFromNib 中的UILabel 的扩展名内设置font,并将此扩展名设置为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

      【讨论】:

        【解决方案4】:

        您需要在系列中使用bold 字体的名称。想知道是否有bold版本的American Typewriter,试试输出

        [UIFont fontNamesForFamilyName:@"AmericanTypewriter"] 
        

        到控制台。

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

        [UIFont fontNamesForFamilyName:@"AmericanTypewriter-Bold"] 
        

        【讨论】:

        • @flashcards - 不,我试过你的逻辑,但我的应用程序崩溃了。
        • 嗯...看起来 fontNamesForFamilyName 是 UIFont 上的一个静态方法,它接受一个姓氏。我现在会修正我的答案。你试过用“AmericanTypewriter-Bold”吗?
        • @sagar - “美国打字机”之间没有空格。我认为您在它们之间留出了空间。
        • 确实,我刚试过,应该是[UIFont fontWithName:@"AmericanTypewriter-Bold" size:18.f]
        • 我认为如果我错误地指定了 fontWithName,我会得到一个错误。然而 iOS 只是默认为标准字体。可以获取字体名称和字体列表here
        【解决方案5】:

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

        【讨论】:

          【解决方案6】:

          如何使用 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 没有使用系统字体。
          猜你喜欢
          • 2013-07-08
          • 2023-03-07
          • 1970-01-01
          • 1970-01-01
          • 2013-01-20
          • 1970-01-01
          • 2015-08-28
          • 2012-02-26
          相关资源
          最近更新 更多