Attributed string with custom fonts in storyboard does not load correctly

后端 未结 16 1791
梦谈多话
梦谈多话 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:25

    The fix for me was to use an IBDesignable class:

    import UIKit
    
    @IBDesignable class TIFAttributedLabel: UILabel {
    
        @IBInspectable var fontSize: CGFloat = 13.0
    
        @IBInspectable var fontFamily: String = "DIN Light"
    
        override func awakeFromNib() {
            var attrString = NSMutableAttributedString(attributedString: self.attributedText)
            attrString.addAttribute(NSFontAttributeName, value: UIFont(name: self.fontFamily, size: self.fontSize)!, range: NSMakeRange(0, attrString.length))
            self.attributedText = attrString
        }
    }
    

    Giving you this in the Interface Builder:

    Interface Builder custom font with attributed string

    You can set up your attributedstring just as you normal do, but you'll have to set your fontsize and fontfamily once again in the new available properties.

    As the Interface Builder is working with the custom font by default, this results in a what you see is what you get, which I prefer when building apps.

    Note

    The reason I'm using this instead of just the plain version is that I'm setting properties on the attributed label like the linespacing, which are not available when using the plain style.

    0 讨论(0)
  • 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
    }
    
    0 讨论(0)
  • 2021-01-30 04:27

    In case of attributed string you can add custom font in font list as - Click on font icon this will display following dialog .In the following dialog you can add your own category or existing one for custom font.attributed font dialog

    After it click on Manage Fonts it open the following dialog select category in you created or existing one . Click on + sign to add font in the category. Manage font dialog

    0 讨论(0)
  • 2021-01-30 04:30

    Try this it will work

    In my case when i try to set "Silversky Technology" as Attributed text for label from interface builder its not show when i run in simulator but its show in interface builder. So i used one trick i made Silversky font with 1 pixel bigger then Technology text.

    Attribute text have issue with same size of font so change size of 1 word this thing work with me.

    May be this is xcode bug but this trick work for me.

    0 讨论(0)
提交回复
热议问题