UILabel - setting font - typeface programmatically in iPhone

后端 未结 6 958
谎友^
谎友^ 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:23

    Swift update

    If you've already added the font to the app (as explained here) you can use Swift's extension feature with, as example, a UILabel and the Roboto font:

    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-LightItalic and Roboto-Light are the TTF's font file names. Then, you can simply call

    someUILabel.setFont(style: UILabel.FontStyle.normal, size: 20)
    
    0 讨论(0)
  • 2020-12-05 03:28

    Just NSLog The font you want get:

    NSLog(@" %@", [UIFont fontNamesForFamilyName:@"American Typewriter"]);
    

    And you get the array:

    (
        "AmericanTypewriter-CondensedLight",
        "AmericanTypewriter-Light",
        "AmericanTypewriter-Bold",
        AmericanTypewriter,
        "AmericanTypewriter-CondensedBold",
        "AmericanTypewriter-Condensed"
    )
    

    Use any you need in 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];
    
    0 讨论(0)
  • 2020-12-05 03:30

    You will need to use the name of the bold font within the family. To find out if there is a bold version of American Typewriter, try outputting

    [UIFont fontNamesForFamilyName:@"AmericanTypewriter"] 
    

    to the console.

    In this case, you should use "AmericanTypewriter-Bold".

    [UIFont fontNamesForFamilyName:@"AmericanTypewriter-Bold"] 
    
    0 讨论(0)
  • 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

    0 讨论(0)
  • 2020-12-05 03:40

    What about using the setter property:

    // 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 讨论(0)
  • 2020-12-05 03:44

    You can get all the iOS supported font names in this site iOSfonts. Take preview of your exact string to find the best font and use it like mentioned in above answers

    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 讨论(0)
提交回复
热议问题