xeb
xeb

Reputation: 201

Custom Font not working with size class

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

Upvotes: 0

Views: 1024

Answers (3)

Kadian
Kadian

Reputation: 654

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

Upvotes: 1

Ashokkumar Adichill
Ashokkumar Adichill

Reputation: 238

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
    }

Upvotes: 1

Rashed
Rashed

Reputation: 2425

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

Upvotes: 1

Related Questions