xcode 在 Interface Builder 中使用自定义字体设计标签/文本视图

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9683929/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 23:37:50  来源:igfitidea点击:

Designing labels/text views with custom fonts in Interface Builder

iosxcodefontsinterface-buildercustom-font

提问by bompf

I am currently working on an iPhone project in which all labels/text views etc. should appear with a custom font (I'm using Xcode 4.2.1). I have done some research on this, and the only solution seems to be adding the font files to the project, editing the info.plist file appropriately and setting the font programmatically (i.e. by outlets or by subclassing the relevant views). In any case, these approaches won't lead to the interface builder displaying the text with the custom font (it will still show Helvetica). It is not possible to select the custom font using the attributes inspector.

我目前正在开发一个 iPhone 项目,其中所有标签/文本视图等都应该以自定义字体显示(我使用的是 Xcode 4.2.1)。我对此做了一些研究,唯一的解决方案似乎是将字体文件添加到项目中,适当地编辑 info.plist 文件并以编程方式设置字体(即通过插座或通过子类化相关视图)。在任何情况下,这些方法都不会导致界面构建器使用自定义字体显示文本(它仍将显示 Helvetica)。无法使用属性检查器选择自定义字体。

Although I don't think I missed something, I just want to make sure there is no other way than programmatically setting the fonts, which would be a rather painful for the task I have to work on (localization issues, unique app design etc.). It would be nice to get the interface builder to display custom fonts..

虽然我不认为我遗漏了什么,但我只是想确保除了以编程方式设置字体之外别无他法,这对于我必须处理的任务(本地化问题、独特的应用程序设计等)来说将是一项相当痛苦的工作。 )。让界面构建器显示自定义字体会很好..

回答by Shubhank

Custom fonts can only be set programmatically..Unfortunately..this is termed as a bug in interface builder and hasn't been fixed yet.

自定义字体只能以编程方式设置..不幸的是..这被称为界面构建器中的错误,尚未修复。

回答by abdus.me

using custom fonts in Interface Buildera reusable library is available.

在 Interface Builder 中使用自定义字体可以使用可重用的库。

It uses a simple trick for doing this. Set the font(say calibri) to all of your UI Elements that you will never use in your project and FontReplacer will do mapping between custom font and calibri. So there is no need to make IBOutlets or any other graphics.

它使用一个简单的技巧来做到这一点。将字体(比如 calibri)设置为您永远不会在项目中使用的所有 UI 元素,FontReplacer 将在自定义字体和 calibri 之间进行映射。所以没有必要制作IBOutlets或任何其他图形。

Here is link to github from where you can download FontReplacer to use in your project. https://github.com/0xced/FontReplacer

这是 github 的链接,您可以从那里下载 FontReplacer 以在您的项目中使用。https://github.com/0xced/FontReplacer

another question is addressing same Fonts not displaying in Interface Builderi have also posted possible solution there

另一个问题是解决未在界面生成器中显示的相同 字体我也在那里发布了可能的解决方案

回答by Alexandre G

As of XCode 6 it now sees the font that I needed to import (Lato).

从 XCode 6 开始,它现在可以看到我需要导入的字体(Lato)。

Haven't tested it much, but works for plain text labels and doesn't for attributed ones. Atrributed shows fine in Interface Builder but defaults to system font at runtime.

尚未对其进行过多测试,但适用于纯文本标签,不适用于属性标签。Atrtributed 在 Interface Builder 中显示良好,但在运行时默认为系统字体。

Still much better to have it there visually or combine text labels to achieve "attributed" text though!

尽管如此,最好还是把它放在视觉上或结合文本标签来实现“属性”文本!

回答by Ashish Chhabra

Applicable for iOS 5.0 & Above.

适用于 iOS 5.0 及以上。

Create a custom UI-Element(UIButton, UILabel, UITextFieldetc) class & simply change the class name for the particular element in storyboard for which you want to reflect the change.

创建一个自定义UI的元素(UIButtonUILabelUITextField等)类和简单地改变类名在故事板您要反映改变特定元素。

Below is the code for custom button. You can do the same for any other element you want.

下面是自定义按钮的代码。您可以对您想要的任何其他元素执行相同的操作。

CustomButtton.h

@interface CustomButtton : UIButton

@end

CustomButtton.m

@implementation CustomButtton

    - (void)awakeFromNib {
        // Initialization code

        if ([self isFontBold:self]) {

            self.titleLabel.font = [UIFont fontWithName:@"LaoUI-Bold" size:self.titleLabel.font.pointSize];

        } else {

            self.titleLabel.font = [UIFont fontWithName:@"LaoUI" size:self.titleLabel.font.pointSize];
        }

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

    -(BOOL)isFontBold:(UIButton *)sender {

        UIFont *font = self.titleLabel.font;
        UIFontDescriptor *fontDescriptor = font.fontDescriptor;
        UIFontDescriptorSymbolicTraits fontDescriptorSymbolicTraits = fontDescriptor.symbolicTraits;
        return (fontDescriptorSymbolicTraits & UIFontDescriptorTraitBold);
    }

@end

Now simply set the font size & type (Bold or Normal). It will automatically update the element with custom font.

现在只需设置字体大小和类型(粗体或普通)。它将使用自定义字体自动更新元素。

By : [UIFont familyNames] - You can check whether your custom font family is included by the application or not which you have defined in .plist file.

By : [UIFont familyNames] - 您可以检查您在 .plist 文件中定义的应用程序是否包含您的自定义字体系列。

Secondly: You have to add a key in .plist file of your project as shown in below screenshot:

其次:您必须在项目的 .plist 文件中添加一个密钥,如下图所示:

.plist change for custom font support in application

.plist change for custom font support in application

回答by Phu Nguyen

It's really inconvenient. While waiting for this feature, I think this will help you organize your code regarding the font in iOS project.

真的很不方便。在等待此功能时,我认为这将帮助您组织有关 iOS 项目中字体的代码。

In the shared class you use to define some global variables like Common.h, you define the fonts you may use

在你用来定义一些全局变量的共享类中,比如 Common.h,你定义了你可以使用的字体

#define FONT_LATO_REGULAR(s) [UIFont fontWithName:@"Lato-Regular" size:s]
#define FONT_LATO_LIGHT(s) [UIFont fontWithName:@"Lato-Light" size:s]
#define FONT_LATO_BOLD(s) [UIFont fontWithName:@"Lato-Bold" size:s]

Then you import the Common.h (your shared class) into class you are implementing, set font for label by:

然后将 Common.h(您的共享类)导入到您正在实现的类中,通过以下方式设置标签字体:

_lblTitle.font = FONT_LATO_BOLD(14.0);

Finally, you can put all font settings into a method for further modification.

最后,您可以将所有字体设置放入一个方法中进行进一步修改。

- (void) setFontForLabels {
    _lblTitle.font = FONT_LATO_BOLD(14.0);
    _lblTime.font = FONT_LATO_REGULAR(13.0);
    _lblLocation.font = FONT_LATO_REGULAR(13.0);
}

It may help you a little. Please make sure you imported your fonts into splist file already.

它可能会帮助你一点。请确保您已经将字体导入到 splist 文件中。

回答by Taryn

I haven't tried this with earlier versions, but with XCode 6, If you have the font installed correctly in your project AND you install the fonts on your Mac, by using Font Book for instance, XCode shows the fonts in the font picker, allows you to set multiple custom fonts and sizes in a single label, and seems to actually use the correct fonts at runtime.

我没有在早期版本中尝试过这个,但是在 XCode 6 中,如果您在项目中正确安装了字体并且在 Mac 上安装了字体,例如通过使用 Font Book,XCode 会在字体选择器中显示字体,允许您在单个标签中设置多个自定义字体和大小,并且似乎在运行时实际上使用了正确的字体。

It's not all roses. Interface Builder's attribute controls are a still a mess. It frequently loses track of the assigned properties, particularly if you try to attribute more than a single paragraph at once. Font colors don't stick very well. Overlapping selections just confuse it incredibly, for instance if your label has two paragraphs, with two different fonts and you select both and change the font size, it may set the attributes of one or both paragraphs to a system default.

不全是玫瑰。Interface Builder 的属性控件还是一团糟。它经常会丢失对指定属性的跟踪,尤其是当您尝试一次为多个段落添加属性时。字体颜色不太好。重叠的选择只会令人难以置信地混淆它,例如,如果您的标签有两个段落,使用两种不同的字体,并且您选择两者并更改字体大小,它可能会将一个或两个段落的属性设置为系统默认值。

回答by monjer

You can set custom font in the storyboard or xib with attibutes inspector, please ref to Custom font in a storyboard?

您可以使用属性检查器在故事板或 xib 中设置自定义字体,请参阅故事板中的自定义字体?

回答by Muhammad Ali

You can use a simple trick to use custom fonts in interface builder. Here is descriptive tutorial that might help you in this regard Click Here for tutorial

您可以使用一个简单的技巧在界面构建器中使用自定义字体。这是描述性教程,可能在这方面对您有所帮助 单击此处获取教程