开发者

Why can't I change font in interface builder?

开发者 https://www.devze.com 2023-03-19 11:13 出处:网络
I change font to some other installed font in interface builder, and it always remains h开发者_StackOverflow中文版elvetica.

I change font to some other installed font in interface builder, and it always remains h开发者_StackOverflow中文版elvetica. Do I need to have developer license to change fonts or it's something else?


Here you can use only those fonts which are provided in fontFamily. Use the following code to find the supported font by iOS device.

NSLog(@"iOS Fonts: %@",[UIFont familyNames]);


iOS Fonts: (
    Thonburi,
    "Snell Roundhand",
    "Academy Engraved LET",
    "Marker Felt",
    "Geeza Pro",
    "Arial Rounded MT Bold",
    "Trebuchet MS",
    Arial,
    "Gurmukhi MN",
    "Malayalam Sangam MN",
    "Bradley Hand",
    "Kannada Sangam MN",
    "Bodoni 72 Oldstyle",
    Cochin,
    "Sinhala Sangam MN",
    "Hiragino Kaku Gothic ProN",
    Papyrus,
    Verdana,
    "Zapf Dingbats",
    Courier,
    "Hoefler Text",
    Helvetica,
    "Hiragino Mincho ProN",
    "Bodoni Ornaments",
    "Apple Color Emoji",
    Optima,
    "Gujarati Sangam MN",
    "Devanagari Sangam MN",
    "Times New Roman",
    Kailasa,
    "Telugu Sangam MN",
    "Heiti SC",
    Futura,
    "Bodoni 72",
    Baskerville,
    "Chalkboard SE",
    "Heiti TC",
    Copperplate,
    "Party LET",
    "American Typewriter",
    AppleGothic,
    "Bangla Sangam MN",
    Noteworthy,
    Zapfino,
    "Tamil Sangam MN",
    "DB LCD Temp",
    "Arial Hebrew",
    "Heiti K",
    Georgia,
    "Heiti J",
    "Helvetica Neue",
    "Gill Sans",
    Chalkduster,
    Palatino,
    "Courier New",
    "Oriya Sangam MN",
    Didot,
    "Bodoni 72 Smallcaps"
)

For the font other then this one you need to add custom font in your applicaiton. You can find the code for Custom Font in iOS here.


This works for me. Replace _someView with your the parent view that contains the buttons and labels you want to set the font for. In this example, I slam all the UILabels and UIButtons to a specific font. Using -[UIView subviews] allows me to avoid outlets for every single item. Also, note I preserve the font size for each item:

- (void) viewDidLoad
{
    NSArray    *allSubviews;

    allSubviews = [_someView subviews];
    for (UIView *subview in allSubviews)
    {
        if (([subview isKindOfClass: [UILabel class]]) || ([subview isKindOfClass: [UIButton class]]))
        {
            UIFont     *font;
            CGFloat    pointSize;

            font = [(UILabel *) subview font];
            pointSize = font.pointSize;
            [(UILabel *) subview setFont: [UIFont fontWithName: @"Bodoni 72 Oldstyle" size: pointSize]];
        }
    }
}


You can make this a little easier on yourself by making a subclass of UILabel (or UIButton) and setting the font in its initWithCoder method, which is called when nibs are unpacked. This means you can then choose this subclass instead of UILabel in Interface Builder, and you have the freedom change your font size easily there, rather than doing it in code.

Here's my example to use the Sabon font:

In the .h:

@interface UILabelSabon : UILabel {

}

@end

And the .m is just:

- (id)initWithCoder:(NSCoder *)decoder {

    if ((self = [super initWithCoder: decoder])) {

        [self setFont: [UIFont fontWithName: @"Sabon" size: self.font.pointSize]];
    }
    return self;
}


You just need to download your font's ttf file and after that drag n drop this file to xcode resources folder. select info.plist file -right click and select open as source code and then add this line:-

<key>UIAppFonts</key>
<array>
    <string>yourfontfilename.ttf </string>
</array>

and access your new font as

 [UIFont fontWithName:@"yrfontname" size:45]  //remember this fontname of yr font is not file name it is the name of font that can be read by opening this font on font book on mac
0

精彩评论

暂无评论...
验证码 换一张
取 消