Changing Font Size in a Label for Only iPhone 4S, Is This Possible

Changing font size in a label for only iPhone 4s, is this possible?

Detecting the device model:

if UIDevice.currentDevice().model == "iPhone4,1" {
// do whatever you want here for iPhone 4S
} else {
// other devices
}

nativeBounds

The bounding rectangle of the physical screen, measured in pixels.
(read-only)

Declaration SWIFT

  var nativeBounds: CGRect { get }

This rectangle is based on the device in a portrait-up orientation. This
value does not change as the device rotates.

Import Statement

import UIKit

Detecting the device's height:

if UIScreen.mainScreen().nativeBounds.height == 960.0 {

}

Detecting the device's width:

if UIScreen.mainScreen().nativeBounds.width == 640.0 {

}

how to make the label proportional to the screen size?

You can do it by setting both proportional width of the label and allow it to scale down the point size.

Set the label width to proportional width like this. It is just a regular same width constraint that you edit.

Sample Image

Then allow your label to scale down the point size like this. This is a property of your label.

Sample Image

Dynamically changing font size of UILabel

Single line:

factLabel.numberOfLines = 1;
factLabel.minimumFontSize = 8;
factLabel.adjustsFontSizeToFitWidth = YES;

The above code will adjust your text's font size down to (for example) 8 trying to fit your text within the label.
numberOfLines = 1 is mandatory.

Multiple lines:

For numberOfLines > 1 there is a method to figure out the size of final text through NSString's sizeWithFont:... UIKit addition methods, for example:

CGSize lLabelSize = [yourText sizeWithFont:factLabel.font
forWidth:factLabel.frame.size.width
lineBreakMode:factLabel.lineBreakMode];

After that you can just resize your label using resulting lLabelSize, for example (assuming that you will change only label's height):

factLabel.frame = CGRectMake(factLabel.frame.origin.x, factLabel.frame.origin.y, factLabel.frame.size.width, lLabelSize.height);

iOS6

Single line:

Starting with iOS6, minimumFontSize has been deprecated. The line

factLabel.minimumFontSize = 8.;

can be changed to:

factLabel.minimumScaleFactor = 8./factLabel.font.pointSize;

iOS7

Multiple lines:

Starting with iOS7, sizeWithFont becomes deprecated.
Multiline case is reduced to:

factLabel.numberOfLines = 0;
factLabel.lineBreakMode = NSLineBreakByWordWrapping;
CGSize maximumLabelSize = CGSizeMake(factLabel.frame.size.width, CGFLOAT_MAX);
CGSize expectSize = [factLabel sizeThatFits:maximumLabelSize];
factLabel.frame = CGRectMake(factLabel.frame.origin.x, factLabel.frame.origin.y, expectSize.width, expectSize.height);

iOS 13 (Swift 5):

label.adjustsFontSizeToFitWidth = true
label.minimumScaleFactor = 0.5

Changing label text size in code

It may be useful to you to use:

[label setFont:[UIFont systemFontOfSize:35]];

or

[label setFont: [UIFont fontWithName:@"Arial" size:50.0]];

iOS Different Font Sizes within Single Size Class for Different Devices

Edit: I don't recommend this anymore. This approach doesn't scale well when new devices come out. Use a combination of dynamic font sizes and size classes-specific fonts.


Say a new iPhone model comes out, if you are using Auto Layout and Size Classes you don't have to fix all the constraints manually to make your app compatible with this newer device. However, you can still set the font size of the UILabel using the following code:

if UIScreen.mainScreen().bounds.size.height == 480 {
// iPhone 4
label.font = label.font.fontWithSize(20)
} else if UIScreen.mainScreen().bounds.size.height == 568 {
// IPhone 5
label.font = label.font.fontWithSize(20)
} else if UIScreen.mainScreen().bounds.size.width == 375 {
// iPhone 6
label.font = label.font.fontWithSize(20)
} else if UIScreen.mainScreen().bounds.size.width == 414 {
// iPhone 6+
label.font = label.font.fontWithSize(20)
} else if UIScreen.mainScreen().bounds.size.width == 768 {
// iPad
label.font = label.font.fontWithSize(20)
}

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:

Sample Image

Sample Image

Try this:

Sample Image

Sample Image

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

Sample Image

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