XCode - 更改情节提要中的默认全局字体

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40035083/
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-15 09:27:36  来源:igfitidea点击:

XCode - Change Default Global Font in Storyboard

iosxcodestoryboard

提问by tika

Very much like how we can set the Global Tintin Storyboard, Is it possible to set the global font-family as something else? For example, I want to change the Global/Default Fontfrom System to Source Sans Pro 16pt. However, what I have to do (to my knowledge)is one of the following:

非常像我们如何在 Storyboard 中设置全局色调,是否可以将全局字体系列设置为其他东西?例如,我想将全局/默认字体从 System 更改为Source Sans Pro 16pt. 但是,我必须做的(据我所知)是以下之一:

  1. Change font of each label, button, textField, etc. in Storyboard.
  2. Set it via Swift ViewDidLoad Code (like this question) or through extensions as explained in this question
  1. 更改 Storyboard 中每个标签、按钮、文本字段等的字体。
  2. 通过 Swift ViewDidLoad 代码(如本问题)或通过本问题中解释的扩展进行设置

My Problem with (2) is that I do not get Visual Feedbacks like in (1) using storyboards. On the other hand, it is also not very eloquent as I have to manually set it anyway.

我对 (2) 的问题是我没有像 (1) 那样使用故事板获得视觉反馈。另一方面,它也不是很有说服力,因为无论如何我都必须手动设置它。

So, is there a way to change/set the default Storyboard font?

那么,有没有办法更改/设置默认的 Storyboard 字体?

采纳答案by Carien van Zyl

You can use the Appearance API, to ensure that all controls have the same font (at runtime)

您可以使用 Appearance API,以确保所有控件具有相同的字体(在运行时)

Look at this questionas to how to set the font for UIButton with Appearance.

看看这个关于如何为带有外观的 UIButton 设置字体的问题

For UILabel and UITextView, do the following. This can be done in AppDelegate application(_:didFinishLaunchingWithOptions:)

对于 UILabel 和 UITextView,请执行以下操作。这可以在 AppDelegate 中完成application(_:didFinishLaunchingWithOptions:)

let labelAppearance = UILabel.appearance()
labelAppearance.font = UIFont.myFont()

let textFieldAppearance = UITextView.appearance()
textFieldAppearance.font = UIFont.myFont()

The previous solution, however will not update storyboard.

但是,以前的解决方案不会更新故事板。

To see the changes visually on storyboard, you can look into the function

要在故事板上直观地查看更改,您可以查看功能

prepareForInterfaceBuilder()

Hereis an answer that explains how to get it visually updated in storyboard, but for this you will need to use custom classes for your textFields, buttons, etc.

是一个解释如何在情节提要中对其进行视觉更新的答案,但为此您需要为文本字段、按钮等使用自定义类。

Code Example as per above link:

上面链接中的代码示例:

@IBDesignable
public class MyUILabel: UILabel {

    public override func awakeFromNib() {
        super.awakeFromNib()
        configureLabel()
    }

    public override func prepareForInterfaceBuilder() {
        super.prepareForInterfaceBuilder()
        configureLabel()
    }

    func configureLabel() {
        font = UIFont(name: Constants.DefaultFont, size: 40)
    }

}