Attributed string with custom fonts in storyboard does not load correctly

后端 未结 16 1997
梦谈多话
梦谈多话 2021-01-30 03:35

We are using custom fonts in our project. It works well in Xcode 5. In Xcode 6, it works in plain text, attributed string in code. But those attributed strings set in storyboard

16条回答
  •  無奈伤痛
    2021-01-30 04:27

    Thanks to this thread, I've come to this solution:

    private let fontMapping = [
        "HelveticaNeue-Medium": "ITCAvantGardePro-Md",
        "HelveticaNeue": "ITCAvantGardePro-Bk",
        "HelveticaNeue-Bold": "ITCAvantGardePro-Demi",
    ]
    
    func switchFontFamily(string: NSAttributedString) -> NSAttributedString {
        var result = NSMutableAttributedString(attributedString: string)
        string.enumerateAttribute(NSFontAttributeName, inRange: NSRange(location: 0, length: string.length), options: nil) { (font, range, _) in
            if let font = font as? UIFont {
                result.removeAttribute(NSFontAttributeName, range: range)
                result.addAttribute(NSFontAttributeName, value: UIFont(name: fontMapping[font.fontName]!, size: font.pointSize)!, range: range)
            }
        }
        return result
    }
    

提交回复
热议问题