Custom Font Sizing in Xcode 6 Size Classes not working properly with Custom Fonts

前端 未结 14 1279
隐瞒了意图╮
隐瞒了意图╮ 2020-11-29 17:42

Xcode 6 has a new feature where fonts and font sizes in UILabel, UITextField, and UIButton can be s

14条回答
  •  暖寄归人
    2020-11-29 18:35

    A combination of some of the later answers above were helpful. Here's how I solved the IB bug via a Swift UILabel extension:

    import UIKit
    
    // This extension is only required as a work-around to an interface builder bug in XCode 7.3.1
    // When custom fonts are set per size class, they are reset to a small system font
    // In order for this extension to work, you must set the fonts in IB to System
    // We are switching any instances of ".SFUIDisplay-Bold" to "MuseoSans-700" and ".SFUIDisplay-Regular" to "MuseoSans-300" and keeping the same point size as specified in IB
    
    extension UILabel {
        override public func traitCollectionDidChange(previousTraitCollection: UITraitCollection?) {
            super.traitCollectionDidChange(previousTraitCollection)
    
            if ((traitCollection.verticalSizeClass != previousTraitCollection?.verticalSizeClass) || traitCollection.horizontalSizeClass != previousTraitCollection?.horizontalSizeClass) {
                //let oldFontName = "\(font.fontName)-\(font.pointSize)"
    
                if (font.fontName == systemFontRegular) {
                    font = UIFont(name: customFontRegular, size: (font?.pointSize)!)
                    //xlog.debug("Old font: \(oldFontName) -> new Font: \(font.fontName) - \(font.pointSize)")
                }
                else if (font.fontName == systemFontBold) {
                    font = UIFont(name: customFontBold, size: (font?.pointSize)!)
                    //xlog.debug("Old font: \(oldFontName) -> new Font: \(font.fontName) - \(font.pointSize)")
                }
            }
        }
    }
    

提交回复
热议问题