ios 你如何在 Swift 中风格化字体?

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

How do you stylize a font in Swift?

iosswiftuifont

提问by user3763693

I'm trying out developing for Swift, it's going pretty well. One of the issues I'm having is finding out how to stylize fonts programmatically in the language.

我正在尝试为 Swift 进行开发,进展顺利。我遇到的问题之一是找出如何在该语言中以编程方式对字体进行样式化。

For example in this label I wrote the code below for, how can I make it Helvetica Neue Ultralight for example?

例如,在这个标签中,我写了下面的代码,例如,我怎样才能使它成为 Helvetica Neue Ultralight?

label.font = UIFont (name: "Helvetica Neue", size: 30)

回答by KRUKUSA

I am assuming this is a custom font. For any custom font this is what you do.

我假设这是一个自定义字体。对于任何自定义字体,这就是您要做的。

  1. First download and add your font files to your project in Xcode (The files should appear as well in “Target -> Build Phases -> Copy Bundle Resources”).

  2. In your Info.plist file add the key “Fonts provided by application” with type “Array”.

  3. For each font you want to add to your project, create an item for the array you have created with the full name of the file including its extension (e.g. HelveticaNeue-UltraLight.ttf). Save your “Info.plist” file.

  1. 首先在 Xcode 中下载字体文件并将其添加到您的项目中(这些文件也应该出现在“Target -> Build Phases -> Copy Bundle Resources”中)。

  2. 在您的 Info.plist 文件中,添加类型为“Array”的“应用程序提供的字体”键。

  3. 对于要添加到项目中的每种字体,请为您创建的数组创建一个项目,并使用文件的全名包括其扩展名(例如 HelveticaNeue-UltraLight.ttf)。保存您的“Info.plist”文件。

label.font = UIFont (name: "HelveticaNeue-UltraLight", size: 30)

label.font = UIFont (name: "HelveticaNeue-UltraLight", size: 30)

回答by Undo

A great resource is iosfonts.com, which says that the name for that font is HelveticaNeue-UltraLight. So you'd use this code:

iosfonts.com是一个很好的资源,它说该字体的名称是HelveticaNeue-UltraLight. 所以你会使用这个代码:

label.font = UIFont(name: "HelveticaNeue-UltraLight", size: 30)

If the system can't find the font, it defaults to a 'normal' font - I think it's something like 11-point Helvetica. This can be quite confusing, always check your font names.

如果系统找不到字体,它默认为“普通”字体——我认为它类似于 11 点 Helvetica。这可能会令人困惑,请始终检查您的字体名称。

回答by Jayprakash Dubey

You can set custom font in two ways : design timeand run-time.

您可以通过两种方式设置自定义字体:design timerun-time

  1. First you need to download required font(.ttf file format). Then, double click on the file to install it.

  2. This will show a pop-up. Click on 'install font' button.

  1. 首先你需要download required font(.ttf 文件格式)。然后,双击该文件进行安装。

  2. 这将显示一个弹出窗口。单击“ install font”按钮。

Screenshot 1

截图 1

  1. This will install the font and will appear in 'Fonts'window.
  1. 这将安装字体并出现在'Fonts'窗口中。

Screenshot 2

截图 2

  1. Now, the font is installed successfully. Drag and drop the custom font in your project. While doing this make sure that 'Add to targets' is checked.
  1. 至此,字体安装成功。将自定义字体拖放到您的项目中。执行此操作时,请确保Add to targets选中“ ”。

Screenshot 3

截图 3

  1. You need to make sure that this file is also added into 'Copy Bundle Resources'present in Build Phasesof Targetsof your project.
  1. 您需要确保此文件也添加到您的项目Copy Bundle Resources'Build Phases的'中Targets

Screenshot 4

截图 4

  1. After this you need to add this font in Info.plistof your project. Create a new key with 'Font Provided by application' with type as Array. Add a the font as an element with type String in Array.
  1. 在此之后,您需要将此字体添加Info.plist到您的项目中。使用 ' Font Provided by application'创建一个类型为Array的新键。在 Array 中将字体添加为 String 类型的元素。

Screenshot 5

截图 5

A. Design mode :

A. Design mode :

  1. Select the label from Storyboard file. Goto Fontattribute present in Attribute inspectorof Utilities.
  1. 从 Storyboard 文件中选择标签。转到Font属性存在Attribute inspectorUtilities

Screenshot 6

截图 6

  1. Click on Font and select 'Custom font'
  1. 单击字体并选择“自定义字体”

Screenshot 7

截图 7

  1. From Family section select the custom font you have installed.
  1. 从 Family 部分选择您已安装的自定义字体。

Screenshot 8

截图 8

  1. Now you can see that font of label is set to custom font.
  1. 现在您可以看到标签的字体设置为自定义字体。

Screenshot 9

截图 9

B. Run-time mode :

B. Run-time mode :

 self.lblWelcome.font = UIFont(name: "BananaYeti-Extrabold Trial", size: 16)

It seems that run-time mode doesn't work for dynamically formed string like

似乎运行时模式不适用于动态形成的字符串,例如

self.lblWelcome.text = "Welcome, " + fullname + "!"

Note that in above case only design-time approach worked correctly for me.

请注意,在上述情况下,只有设计时方法适合我。

回答by Ankit Goyal

Add Custom Font in Swift

在 Swift 中添加自定义字体

  1. Drag and drop your font in your project.
  2. Double check that it is added in Copy Bundle Resource. (Build Phase -> Copy Bundle Resource).
  3. In your plist file add "Font Provided by application" and add your fonts with full name.
  4. Now use your font like: myLabel.font = UIFont (name: "GILLSANSCE-ROMAN", size: 20)
  1. 将您的字体拖放到您的项目中。
  2. 仔细检查它是否已添加到 Copy Bundle Resource 中。(构建阶段 -> 复制捆绑资源)。
  3. 在您的 plist 文件中添加“由应用程序提供的字体”并使用全名添加您的字体。
  4. 现在使用您的字体,例如: myLabel.font = UIFont (name: "GILLSANSCE-ROMAN", size: 20)

回答by Shanu Singh

Xamarin

沙马林

Label.Font = UIFont.FromName("Copperplate", 10.0f);

Swift

迅速

text.font = UIFont.init(name: "Poppins-Regular", size: 14)

To get the list of font familyGithub/IOS-UIFont-Names

获取字体家族列表Github/IOS-UIFont-Names