简体   繁体   English

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

[英]Designing labels/text views with custom fonts in Interface Builder

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). 我目前正在开发一个iPhone项目,其中所有标签/文本视图等应该以自定义字体出现(我正在使用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 (ie by outlets or by subclassing the relevant views). 我已经对此做了一些研究,唯一的解决方案似乎是将字体文件添加到项目中,适当地编辑info.plist文件并以编程方式设置字体(即通过出口或通过子类化相关视图)。 In any case, these approaches won't lead to the interface builder displaying the text with the custom font (it will still show Helvetica). 在任何情况下,这些方法都不会导致界面构建器使用自定义字体显示文本(它仍将显示Helvetica)。 It is not possible to select the custom font using the attributes inspector. 无法使用属性检查器选择自定义字体。

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.. 让界面构建器显示自定义字体会很不错。

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

using custom fonts in Interface Builder a 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. 将字体(比如calibri)设置为您在项目中永远不会使用的所有UI元素,FontReplacer将在自定义字体和calibri之间进行映射。 So there is no need to make IBOutlets or any other graphics. 所以不需要制作IBOutlets或任何其他图形。

Here is link to github from where you can download FontReplacer to use in your project. 这里是github的链接,您可以从中下载FontReplacer以在项目中使用。 https://github.com/0xced/FontReplacer https://github.com/0xced/FontReplacer

another question is addressing same Fonts not displaying in Interface Builder i have also posted possible solution there 另一个问题是解决在Interface Builder中没有显示的相同字体我还在那里发布了可能的解决方案

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. Atrributed在Interface Builder中显示正常但在运行时默认为系统字体。

Still much better to have it there visually or combine text labels to achieve "attributed" text though! 更好的方法是在视觉上或文本标签组合,以实现“归属”文本!

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(您的共享类)导入到您正在实现的类中,为label设置字体:

_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文件。

Applicable for iOS 5.0 & Above. 适用于iOS 5.0及以上版本。

Create a custom UI-Element( UIButton , UILabel , UITextField etc) class & simply change the class name for the particular element in storyboard for which you want to reflect the change. 创建自定义UI元素( UIButtonUILabelUITextField等)类,只需更改要反映更改的storyboard中特定元素的类名。

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. 通过:[UIFont familyNames] - 您可以检查您的自定义字体系列是否包含在您在.plist文件中定义的应用程序中。

Secondly: You have to add a key in .plist file of your project as shown in below screenshot: 其次:您必须在项目的.plist文件中添加一个键,如下面的屏幕截图所示:

.plist更改为应用程序中的自定义字体支持

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. Interface Builder的属性控件仍然是一团糟。 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. 重叠选择只是令人难以置信,例如,如果您的标签有两个段落,两个不同的字体,您选择两个并更改字体大小,它可能会将一个或两个段落的属性设置为系统默认值。

您可以使用attibutes 检查器在storyboard或xib中设置自定义字体,请在故事板中引用自定义字体?

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 以下是可能对您有所帮助的描述性教程单击此处获取教程

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

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