我正在尝试弄清如何将字体风格更改为“细体”。有人知道如何做吗?
这是我最好的尝试,但它不起作用:
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)]")
}
}
在检测到字体后,您可以像这样设置:
m.font = UIFont(name: "AppleSDGothicNeo-Thin", size: 8.0)
let font = UIFont(name: "HelveticaNeue-Thin", size: 16.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(name: "HelveticaNeue-UltraLight", size: 14.0)
标签lblDes的字体被设置为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