本文介绍了如何制作系统字体“黑色斜体"在迅速?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试添加重量为Heavy"(非粗体)的系统字体,并尝试将其设为斜体.我看到了其他 stackoverflow 解决方案,但似乎不起作用.这是我所做的:
I am trying to add a system font of weight "Heavy" (not bold) and also try to make it italic. I saw other stackoverflow solutions but it does not seem to work. Here is what I have done:
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 as suggested in a stack overflow post
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
}
}
事情是当我尝试了所有建议的解决方案时,粗体和斜体似乎有效,但黑色和斜体不起作用.它仍然呈现为常规斜体.
Thing is when I tried all suggested solutions, bold and italic seems to work BUT black and italic doesn't work. It is still rendered as regular italic.
推荐答案
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)
这篇关于如何制作系统字体“黑色斜体"在迅速?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!