简体   繁体   English

为什么我不能在界面生成器中更改字体?

[英]Why can't I change font in interface builder?

I change font to some other installed font in interface builder, and it always remains helvetica.我在界面生成器中将字体更改为其他已安装的字体,它始终保持为 helvetica。 Do I need to have developer license to change fonts or it's something else?我是否需要拥有开发人员许可证才能更改 fonts 还是其他?

Here you can use only those fonts which are provided in fontFamily.这里只能使用 fontFamily 中提供的 fonts。 Use the following code to find the supported font by iOS device.使用以下代码查找 iOS 设备支持的字体。

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.您可以在 iOS 中找到自定义字体的代码。

This works for me.这对我有用。 Replace _someView with your the parent view that contains the buttons and labels you want to set the font for.将 _someView 替换为包含要为其设置字体的按钮和标签的父视图。 In this example, I slam all the UILabels and UIButtons to a specific font.在此示例中,我将所有 UILabel 和 UIButtons 猛烈抨击为特定字体。 Using -[UIView subviews] allows me to avoid outlets for every single item.使用 -[UIView subviews] 可以让我避免每个项目的出口。 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.您可以通过创建 UILabel(或 UIButton)的子类并在其 initWithCoder 方法中设置字体来让自己更轻松一些,该方法在 nib 解包时调用。 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.这意味着您可以在 Interface Builder 中选择这个子类而不是 UILabel,并且您可以自由地在那里轻松更改字体大小,而不是在代码中进行。

Here's my example to use the Sabon font:这是我使用 Sabon 字体的示例:

In the.h:在.h中:

@interface UILabelSabon : UILabel {

}

@end

And the.m is just:而.m 只是:

- (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.您只需要下载字体的 ttf 文件,然后将此文件拖放到 xcode 资源文件夹。 select info.plist file -right click and select open as source code and then add this line:- select info.plist 文件-右键单击并 select 作为源代码打开,然后添加此行:-

<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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么我不能在Interface Builder中更改视图控制器的视图大小? - Why can't I change the view's size of my view controller in Interface Builder? 为什么不能使用Interface Builder将UIActivityIndi​​catorView放在UITableView上? - Why can't I place a UIActivityIndicatorView on a UITableView by using Interface Builder? 无法在Interface Builder中更改UITableViewCell大小 - Can't change UITableViewCell size in Interface Builder 在Interface Builder中更改UITextView中单个单词的字体 - Change font of individual words in UITextView in Interface Builder 为什么Interface Builder无法更改我的ViewController类型? - Why won't Interface Builder change my type of ViewController? 自定义字体在界面构建器中不起作用! - Custom font doesn't work in interface builder! 为什么不能在Interface Builder中设置自定义UIView的背景颜色? - Why can't I set the background color of my custom UIView in Interface Builder? 为什么我不能通过Interface Builder将左栏按钮项添加到导航项? - Why I can't add left bar button item to Navigation Item through Interface Builder? 当它是文件的所有者时,为什么不能在Interface Builder中编辑UIViewController的属性? - Why can't I edit the properties of UIViewController in Interface Builder when it's the File's Owner? 如何更改在“界面”构建器中创建的UINavigationBar的标题 - How can I change the title of UINavigationBar Created in Interface builder
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM