0

I am using a custom font and I want it to be customised using storyboard and size class. I want to vary it according to screen size of iPad and iPhone, smaller the screen size smaller the font size. But its not changing if I am using this custom font. I am using Xcode 7.

I am trying to do it this way

xeb
  • 191
  • 1
  • 4
  • 12

3 Answers3

1

In Storyboard select the label you want to scale

Please find the below picture

enter image description here

enter image description here

More over from my point all of the iphone devices comes with quality pixels so this affect only a pinch level

you can't expect in ipone x -40size -> iphone 5 - 35size

but if your using device logic you can set whatever size you need to set for iphone 5 and iphone x

this will help you

How to determine the current iPhone/device model?

use the link above and create extension then call the extension like below

modelName = UIDevice.current.modelName

switch modelName {
    case "iPhone SE" , "iPhone 5" , "iPhone 5c" , "iPhone 5s" :

        titleLabel.font = UIFont(name : "Vollkorn-Regular" , size : 20.0)
        name.font = UIFont(name : "OpenSans-SemiBold" , size : 12.0)

        print("i 5 series")
        break
    case "iPhone 8 Plus" :

        titleLabel.font = UIFont(name : "Vollkorn-Regular" , size : 24.0)
        name.font = UIFont(name : "OpenSans-SemiBold" , size : 16.0)

        print("i 8 series")
        break
    default:
        break
    }
1

You need a dynamic font size and width and for that you have to implement this two line of code -

labelName.adjustsFontSizeToFitWidth = true
labelName.minimumScaleFactor = 0.3

And from your story board, change the lines of the label to 0-

enter image description here -

Or programmatically you can add this line of code -

labelName.numberOfLines = 0 // or you can set it 1 or 2
Rashed
  • 2,349
  • 11
  • 26
  • i want to change the font size. not number of lines – xeb Apr 09 '18 at 05:42
  • you are slightly right.but number of line 0 means you can add more than one line. And if you specify it by numberof lines 1 then what happened?. After one line your label will not show anything else. – Rashed Apr 09 '18 at 05:52
  • labelName.adjustsFontSizeToFitWidth = true labelName.minimumScaleFactor = 0.3 ..this two line of code will change the font for different device – Rashed Apr 09 '18 at 06:00
1

Use the below code:

class MyCustomLabel: UILabel {
    override func layoutSubviews() {
        self.font = UIFont.init(name: “YourCustomFont", size: self.font.pointSize)
    }
}
  1. Now Just Change the font from storyboard from your current font to System Font. And select whatever font size you want to use.

enter image description here

Actually self.font.pointSize :- This will help you fetching the font size from the Storyboard (whatever you have set with System font.)

  1. Just change the label class , and use MyCustomLabel in storyboard. Now run the application you will be able to see the changes.

enter image description here

Kadian
  • 644
  • 4
  • 6