简体   繁体   中英

Custom font with size classes in iOS Not working correctly

I'm trying to add 2 different font sizes for iphone and ipad layouts using size classes. It works cool with a default System font but doesn't work with custom font. If I add the second size for wR hR then font looks correctly in interface builder(I even checked xml) but in simulator and on device it becomes System instead of my custom font. But if I remove wR hR(or whatever layout I'm using for another size) then font shows correctly. Any idea how to solve this issue? Thanks!

i'm using xCode7

Steps to set custom font .

1) Set fonts as System for size classes

在此处输入图片说明

2) Subclass UILabel and override "layoutSubviews" method like:

- (void)layoutSubviews
{
  [super layoutSubviews];
  // Implement font logic depending on screen size
  self.font = [UIFont fontWithName:@"CustomFont" size:self.font.pointSize];
}

May be it will help you.

在此处输入图片说明

//Create Custom class of UILabel

class LabelDeviceClass : UILabel{
@IBInspectable var fontBold: Bool = true // add bold bool variable in attributes inspector
@IBInspectable var fontItalic: Bool = true / add italic bool variable in attributes inspector


//this code add font size in attributes inspector so you can give font size 
 @IBInspectable var iPhoneSize:CGFloat = 0 {
    didSet {
        if isPhone() {
            overrideFontSize(fontSize: iPhoneSize)
        }
    }
}

@IBInspectable var iPadSize:CGFloat = 0 {
    didSet {
        if isPad() {
            overrideFontSize(fontSize: iPadSize)
        }
    }
}
 func overrideFontSize(fontSize:CGFloat){
    var currentFontName = "SofiaPro"//font name you want to use

    if fontBold{
        currentFontName = "SofiaPro-Bold"
    }
    else if fontItalic{
        currentFontName = "SofiaPro-Italic"
    }

   if let calculatedFont = UIFont(name: currentFontName, size: fontSize) {
            self.font = calculatedFont
    }


}

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM