How to iOS Item and Font Sizes Scaling as Screen Size

How to fit a font size on each device in Main storyboard of iOS?

You can't do it with storyboard. You only can with code, in your code you check device type using bounds of screen. And change font size for each screen size.

UPDATE:
You can try like this

let screenWidth = UIScreen.main.bounds.size.width
if screenWidth == 640 {
// iPhone 4inch and 3.5inch. Smaller than iPhone 8
// Call change font
}

changing the font size according to each iPhone size

Please use size classes to support different font size for different screens. Here is good tutorial about using size classes. adaptive-layout-tutorial

How do I make text labels scale the font size with different Apple product screens?

You can actually do it in your Storyboard.

1, open File Inspector tab(First tab) on your storyboard, set both Use auto layout and Use size classes check.

2, Then you can go to your label attribute(Forth tab), you will notice that there is a "+" beside, click it to add.

3, Choose the screen size u want like, can refer "Adaptivity and Layout" and then select your font size.

Answer refer to THIS LINK, you can also take a look

How to set different font sizes for different devices using Xcode Storyboard?

Use Size-Class and add size variation for fonts from Attribute Inspector of Label property.

Here are different possible variations, you can set with Size class:

enter image description here

enter image description here

Try this:

enter image description here

enter image description here

Here is (result) preview of font-size variation, in iPhone and iPad

enter image description here

Update

The result you are expecting, may not be possible using IB (Storyboard) but you can try it with following programmatic solution:

extension UIDevice {


enum DeviceType: String {
case iPhone4_4S = "iPhone 4 or iPhone 4S"
case iPhones_5_5s_5c_SE = "iPhone 5, iPhone 5s, iPhone 5c or iPhone SE"
case iPhones_6_6s_7_8 = "iPhone 6, iPhone 6S, iPhone 7 or iPhone 8"
case iPhones_6Plus_6sPlus_7Plus_8Plus = "iPhone 6 Plus, iPhone 6S Plus, iPhone 7 Plus or iPhone 8 Plus"
case iPhoneX = "iPhone X"
case unknown = "iPadOrUnknown"
}

var deviceType: DeviceType {
switch UIScreen.main.nativeBounds.height {
case 960:
return .iPhone4_4S
case 1136:
return .iPhones_5_5s_5c_SE
case 1334:
return .iPhones_6_6s_7_8
case 1920, 2208:
return .iPhones_6Plus_6sPlus_7Plus_8Plus
case 2436:
return .iPhoneX
default:
return .unknown
}
}
}

// Get device type (with help of above extension) and assign font size accordingly.
let label = UILabel()

let deviceType = UIDevice.current.deviceType

switch deviceType {

case .iPhone4_4S:
label.font = UIFont.systemFont(ofSize: 10)

case .iPhones_5_5s_5c_SE:
label.font = UIFont.systemFont(ofSize: 12)

case .iPhones_6_6s_7_8:
label.font = UIFont.systemFont(ofSize: 14)

case .iPhones_6Plus_6sPlus_7Plus_8Plus:
label.font = UIFont.systemFont(ofSize: 16)

case .iPhoneX:
label.font = UIFont.systemFont(ofSize: 18)

default:
print("iPad or Unkown device")
label.font = UIFont.systemFont(ofSize: 20)

}


Related Topics



Leave a reply



Submit