28

I have following code in my Application.

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

Now, I need to know,

======> how to set "Typeface=bold" through code?

sagarkothari
  • 24,520
  • 50
  • 165
  • 235

6 Answers6

36

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"] 
Alex Cio
  • 6,014
  • 5
  • 44
  • 74
devinfoley
  • 1,896
  • 16
  • 20
  • @flashcards - no I tried your logic, but my application crashed. – sagarkothari Aug 19 '09 at 22:07
  • Hmmm...looks like fontNamesForFamilyName is a static method on UIFont that takes in a family name. I will fix my answer now. Did you try using "AmericanTypewriter-Bold"? – devinfoley Aug 19 '09 at 22:17
  • 5
    @sagar - There is no space between "American Typewriter". I think you have put space between them. – Brij Dec 11 '09 at 13:20
  • 1
    Indeed, I just tried and it should be `[UIFont fontWithName:@"AmericanTypewriter-Bold" size:18.f]` – DaGaMs Jun 28 '12 at 19:09
  • I figured that if I misspecified the fontWithName, I would get an error. However iOS just defaults to standard font. Can get a list of font names and typefaces [here](https://gist.github.com/shannoga/1008678) – PhillipOReilly Aug 22 '15 at 23:24
20

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];
dandan78
  • 13,328
  • 13
  • 64
  • 78
dcrawkstar
  • 505
  • 1
  • 5
  • 11
17

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];
Nazir
  • 1,945
  • 24
  • 27
2

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];
geet Sebastian
  • 677
  • 6
  • 12
1

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

Community
  • 1
  • 1
Alex Cio
  • 6,014
  • 5
  • 44
  • 74
0

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)
Community
  • 1
  • 1