Arc*_*hid 5 uikit uifont ios swift
我正在尝试添加粗细为“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
}()
Run Code Online (Sandbox Code Playgroud)
堆栈溢出帖子中建议的扩展
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
}
}
Run Code Online (Sandbox Code Playgroud)
问题是,当我尝试所有建议的解决方案时,粗体和斜体似乎有效,但黑色和斜体不起作用。它仍然呈现为常规斜体。
斯威夫特 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)
}
}
Run Code Online (Sandbox Code Playgroud)
用法:
myLabel.font = UIFont.italicSystemFont(ofSize: 34, weight: .black)
Run Code Online (Sandbox Code Playgroud)
尝试这个
label.font = UIFont.systemFont(ofSize: 32, weight: .black, traits: .traitItalic)
//If you just want Italic then use this
labelInfo.font = UIFont.italicSystemFont(ofSize: 32)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7035 次 |
| 最近记录: |