繁体   English   中英

如何在 Swift 中更改字体样式

[英]How to Change Font Style in Swift

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

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

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

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

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

编辑:

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

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

字体名称有问题。

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

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

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)]")
    }
}

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

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

这可能有效:

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

把它放在操场上以获得所有正确的字体名称,可用(在 oleg 的基础上更新为 Swift 3.0 到 Swift 5.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()

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

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

暂无
暂无

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

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