0
votes

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

2
Have you check if the font is in Your Project -> Build Phase -> Copy Bundle Resources?Nikolay Nankov
Yes it's there in the Bundle ResourcesMohamed Ghebaji

2 Answers

0
votes

Steps to set custom font .

1) Set fonts as System for size classes

enter image description here

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.

0
votes

enter image description here

//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
    }


}

}