ios 如何在 Swift 中更改字体样式

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

How to Change Font Style in Swift

iosswiftuifont

提问by Collin O.

I'm trying to figure out how to change the style of a font to "Thin". Does anyone know how to do this?

我想弄清楚如何将字体的样式更改为“细”。有谁知道如何做到这一点?

Here's my best try, but it doesn't work:

这是我最好的尝试,但它不起作用:

m.font = UIFont(name: "Apple SD Gothic Neo", style: "Thin", size: 8.0)

回答by Caleb Kleveter

The way I have seen it is AppleSDGothicNeo-Thin, No spaces, and a dash style. So your code would be

我所看到的方式是AppleSDGothicNeo-Thin,没有空格,并且是破折号样式。所以你的代码将是

m.font = UIFont(name: "AppleSDGothicNeo-Thin", size: 8.0)

Edit:

编辑:

I have come to understand why you use the font this way.

我开始理解你为什么这样使用字体。

If you add a custom font to your project, it has a name of "SuperAwesomeFont-Light.ttf". So it makes sense that you just use the file name for the name of the font.

如果您向项目添加自定义字体,它的名称为“SuperAwesomeFont-Light.ttf”。因此,您只使用文件名作为字体名称是有道理的。

回答by Oleg Gordiichuk

You have trouble with font name.

字体名称有问题。

At first find out proper name of the font and than use it.

首先找出字体的正确名称,然后使用它。

Firstly print all names of them. And then use. Code example show all installed fonts of the application.

首先打印它们的所有名称。然后使用。代码示例显示应用程序的所有已安装字体。

func printFonts() {
    let fontFamilyNames = UIFont.familyNames()
    for familyName in fontFamilyNames {
        print("------------------------------")
        print("Font Family Name = [\(familyName)]")
        let names = UIFont.fontNamesForFamilyName(familyName)
        print("Font Names = [\(names)]")
    }
}

And after you detect Font you can set this like :

在检测到 Font 后,您可以将其设置为:

m.font = UIFont(name: "AppleSDGothicNeo-Thin", size: 8.0)

回答by Niall Kehoe

This might work:

这可能有效:

let font = UIFont(name: "HelveticaNeue-Thin", size: 16.0)!

回答by LukeSideWalker

Put this in playground to get all correct names of the fonts, available (updated for Swift 3.0 on basis of oleg)

把它放在操场上以获得所有正确的字体名称,可用(在 oleg 的基础上为 Swift 3.0 更新)

//: Playground - noun: a place where people can play

import UIKit

func printFonts() {
    let fontFamilyNames = UIFont.familyNames
    for familyName in fontFamilyNames {
        print("------------------------------")
        print("Font Family Name = [\(familyName)]")
        let names = UIFont.fontNames(forFamilyName: familyName)
        print("Font Names = [\(names)]")
    }
}

printFonts()

回答by Urvish Modi

lblDes.font = UIFont (name: "HelveticaNeue-UltraLight", size: 14.0)

lblDes.font = UIFont(名称:“HelveticaNeue-UltraLight”,大小:14.0)

回答by Pixel Code

let myLabel = UILabel(frame: CGRect(x: 0, y: 0, width: yourWidth, height: yourHeight))
    myLabel.text = "Your Text"
    myLabel.font = UIFont(name: "Name of your font", size: 18)
    self.view.addSubview(emptyMessageLabel)
    myLabel.translatesAutoresizingMaskIntoConstraints = false
    myLabel.centerYAnchor.constraint(equalTo: self.view.centerYAnchor).isActive = true
    myLabel.centerXAnchor.constraint(equalTo: self.view.centerXAnchor).isActive = true