gpt4 book ai didi

ios - 如何在swift中制作系统字体 "Black Italic"?

转载 作者:行者123 更新时间:2023-11-28 07:20:11 26 4
gpt4 key购买 nike

我正在尝试添加粗细为“Heavy”(非粗体)的系统字体,并尝试将其设置为斜体。我看到了其他 stackoverflow 解决方案,但它似乎不起作用。这是我所做的:

let percentageLabel: UILabel = {
let label = UILabel()
label.text = "0"
label.textAlignment = .center
label.textColor = .white
label.font = UIFont.systemFont(ofSize: 32, weight: .heavy, traits: .traitItalic)
return label
}()

堆栈溢出帖子中建议的扩展

extension UIFont {

static func systemFont(ofSize: CGFloat, weight: UIFont.Weight, traits: UIFontDescriptor.SymbolicTraits) -> UIFont? {
let font = UIFont.systemFont(ofSize: ofSize, weight: weight)

if let descriptor = font.fontDescriptor.withSymbolicTraits(traits) {
return UIFont(descriptor: descriptor, size: ofSize)
}

return nil
}
}

事情是当我尝试所有建议的解决方案时,粗体和斜体似乎有效但黑色和斜体不起作用。它仍然呈现为常规斜体。

最佳答案

swift 5.*

extension UIFont {

class func italicSystemFont(ofSize size: CGFloat, weight: UIFont.Weight = .regular)-> UIFont {
let font = UIFont.systemFont(ofSize: size, weight: weight)
switch weight {
case .ultraLight, .light, .thin, .regular:
return font.withTraits(.traitItalic, ofSize: size)
case .medium, .semibold, .bold, .heavy, .black:
return font.withTraits(.traitBold, .traitItalic, ofSize: size)
default:
return UIFont.italicSystemFont(ofSize: size)
}
}

func withTraits(_ traits: UIFontDescriptor.SymbolicTraits..., ofSize size: CGFloat) -> UIFont {
let descriptor = self.fontDescriptor
.withSymbolicTraits(UIFontDescriptor.SymbolicTraits(traits))
return UIFont(descriptor: descriptor!, size: size)
}

}

用法:

myLabel.font = UIFont.italicSystemFont(ofSize: 34, weight: .black)

关于ios - 如何在swift中制作系统字体 "Black Italic"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58466359/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com