简体   繁体   中英

Setting font of UILabel with Interface Builder comes out Helvetica

I'm having a problem setting the font for UILabels and UITextViews with Interface Builder. I'm trying to set the font to Gill Sans. If I set it programmatically it works fine, like this:

    myLabel.font = [UIFont fontWithName:@"Gill Sans" size:24.0];

But if I try setting it with Interface Builder, I get the same behaviour described in this question here iPhone SDK: Interface Builder label font, only shows when editing label , but the Gill Sans is supposedly available on the iPad (and it is, since it works if I set it by code). And if I run it and do this:

    NSLog(@"%@", myLabel.font.fontName);

it prints out "Helvetica".

Usually I wouldn't mind setting it programmatically, but the problem is that this particular class is used in several different places with different nib files to provide different layouts, so I can't have it hardcoded to always use the same font family. And subclassing it for each time it appears would be a huge pain, specially because I want to enable designers to create and change all the layout with interface builder whenever they want, and if they have to tell me what font they want every time so I can hardcode every particular case, that would be very awkward.

Has anyone experienced this problem before? Maybe Interface Builder is limiting me to use the iPhone fonts even though the xib file's target is already set to iPad, but how do I convince it that I'm targeting the iPad?

Thanks in advance, filipe

Ok, apparently this has been reported as a bug with IB 3.2.3:
https://devforums.apple.com/message/236134#236134
This guy says it used to work on 3.2.2, so I'll see if I can downgrade, or I'll just wait for a fix from Apple.

Check out this post, if you do not want to create any IBOutlet.

https://stackoverflow.com/a/6620938/400909

You can only use fonts that are present on the iPhone OS. Check this answer for a way to embed custom fonts in your app.

Check out IBCustomFonts category I wrote: https://github.com/deni2s/IBCustomFonts

IBCustomFonts category allows you to use custom fonts from Interface Builder (IB) when building your iOS apps.

Apps using IBCustomFonts category are approved by Apple App Store (as of September 2013).

No need to use IBOutlets, subclassing of UILabels and UIButtons or change fonts in code.

Tested on iOS6 and iOS7.

If you use the subclassing UILabel approach and also want to keep the weight set in IB then use can do some thing like below. I couldn't find a better way to find out if the existing font is bold, italic or regular

- (void)awakeFromNib
{
    [super awakeFromNib];

    NSString *weight = [self.font.fontName substringFromIndex:[self.font.fontName length] - 5];
    if ( [weight isEqualToString:@"-Bold"] )
    {
        self.font = [UIFont fontWithName:@"MyriadWebPro-Bold" size:self.font.pointSize];
    }
    else if ( [weight isEqualToString:@"-Italic"] )
    {
        self.font = [UIFont fontWithName:@"MyriadWebPro-Italic" size:self.font.pointSize];
    }
    else
    {
        self.font = [UIFont fontWithName:@"MyriadWebPro" size:self.font.pointSize];
    }
}

I created a UILabel subclass that allows you to set a custom font in interface builder:

https://github.com/IntrepidPursuits/IPCustomFontLabel

Cleaning all targets works few times. I had to quit IB and xcode to get the fonts in my last instance. I'm using xcode/IB 3.2.5 and I used chalkduster 13pt

HIH

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM