如何制作系统字体“黑色斜体"在迅速?

编程入门 行业动态 更新时间:2024-10-26 05:20:31
本文介绍了如何制作系统字体“黑色斜体"在迅速?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我正在尝试添加重量为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)

这篇关于如何制作系统字体“黑色斜体"在迅速?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

本文发布于:2023-04-29 03:29:54,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1183995.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:斜体   字体   黑色   系统   quot

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!