Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change font name AND weight in UILabel (iPhone SDK)

Tags:

uilabel

iphone

Maybe I'm looking at the wrong place, however how do I set an UILabel's font and AND its weight?

Looking at the documentation, there seems to be only methods to create an UIFont with a given font name and size, like

[UIFont fontWithName:@"Helvetica" size:22])

OR create a bold font, with

[UIFont boldSystemFontOfSize:22]

How can I use these both together?

like image 208
Rafael Steil Avatar asked Aug 16 '10 15:08

Rafael Steil


People also ask

How do I change the font in Xcode?

Add the Font File to Your Xcode Project To add a font file to your Xcode project, select File > Add Files to “Your Project Name” from the menu bar, or drag the file from Finder and drop it into your Xcode project. You can add True Type Font (. ttf) and Open Type Font (. otf) files.

How do I change the size of labels in Xcode?

You can set the Minimum Font Scale or size in Storyboard/Xib when you set it up in IB under the Attributes inspector. I prefer scale, as it is better at fitting longer text on iPhone 4/5/iPod touches. If you set the size, you can get cut off earlier than with scale.


1 Answers

The documentation for fontWithName:size: states, "...name incorporates both the font family and the specific style information for the font."

So you probably want:

[UIFont fontWithName:@"Helvetica-Bold" size:22];

fontNamesForFamilyName: is handy for getting a list of available fonts for a given family.

For example:

NSArray* fontNames = [UIFont fontNamesForFamilyName:@"Helvetica"];
for( NSString* aFontName in fontNames ) {
    NSLog( @"Font name: %@", aFontName );
}

...which outputs:

Font name: Helvetica-BoldOblique
Font name: Helvetica
Font name: Helvetica-Oblique
Font name: Helvetica-Bold
like image 191
zpasternack Avatar answered Oct 03 '22 14:10

zpasternack