【问题标题】:How to make system font "Black Italic" in swift?如何快速制作系统字体“黑色斜体”?
【发布时间】:2020-02-16 08:47:19
【问题描述】:

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

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

【问题讨论】:

    标签: ios swift uikit uifont


    【解决方案1】:

    斯威夫特 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)
    

    【讨论】:

      【解决方案2】:

      Swift 5.0

      extension UIFont {
          static func systemFontItalic(size fontSize: CGFloat = 17.0, fontWeight: UIFont.Weight = .regular) -> UIFont {
              let font = UIFont.systemFont(ofSize: fontSize, weight: fontWeight)
              return UIFont(descriptor: font.fontDescriptor.withSymbolicTraits(.traitItalic)!, size: fontSize)
          }
      }
      

      用法:

      label.font = UIFont.systemFontItalic(size: 20.0, fontWeight: .black)
      

      输出:

      【讨论】:

      • 您好,试过了,还是不行。你觉得是模拟器的问题吗?
      • 试过了,你的扩展会忽略字体粗细
      【解决方案3】:

      试试这个

           label.font = UIFont.systemFont(ofSize: 32, weight: .black, traits: .traitItalic)
      //If you just want Italic then use this
         labelInfo.font = UIFont.italicSystemFont(ofSize: 32)
      

      【讨论】:

        猜你喜欢
        • 2011-06-14
        • 1970-01-01
        • 2020-10-03
        • 2013-12-03
        • 1970-01-01
        • 2021-12-12
        • 2011-10-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多