UILabel - setting font - typeface programmatically in iPhone

后端 未结 6 973
谎友^
谎友^ 2020-12-05 02:40

I have following code in my Application.

tmp=[[UILabel alloc] initWithFrame:label1Frame];
tmp.tag=1;
tmp.textColor=[UIColor blackColor];
[tmp setFont:[UIFont         


        
6条回答
  •  难免孤独
    2020-12-05 03:39

    Like @devinfoley wrote, you have to add -Bold to the fontName you're using. For me it doesn't work with fontNamesForFamilyName, instead I'm using fontWithName:size. Maybe it works if you create the UILabel programmatically. In my case I'm just setting the font inside the extension of my UILabel in awakeFromNib and set this extension as class in the Identity Inspector for my specific UILabel. With self.font.pointSize you can set the fontSize inside the Interface Builder and can handle it better if you have more UI elements.

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

    If your font doesn't work you should print all fonts of the family, so you can see if you wrote the fontName wrong. This way you also can check if there is a bold font available, if its not printed you don't have this option.

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

    More font stuff: https://stackoverflow.com/a/8529661/1141395

提交回复
热议问题