Attributed string with custom fonts in storyboard does not load correctly

You can add custom fonts to font book.

Step1: Click on manage fonts. It opens the font book.

enter image description here

Step2: Click on plus and add your fonts.

enter image description here

Next time when you click on font with attributed text newly added font also will show in the list. But make sure your custom font added in info.plist and bundle resources.


The simplest answer that worked for is to drag the fonts into FontBook. If the fonts are in your project but not in your computer's FontBook, IB sometimes has trouble finding it. Weird, but has worked for me on several occasions.


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.