Different font size for different devices in Xcode 6
Select your text field in your storyboard. In the attributes inspector you will see a small '+' to the left of the font selector. This will allow you to choose size-class specific stuff for your text.
Size classes were added to iOS 8 when Apple introduced Universal Storyboards which let you design for both iPad and iPhone with the same Storyboard file. At the bottom of the Storyboard design window you'll see the (default) 'w Any h Any' button. This gives you access to combinations of the compact and regular size classes. These available combinations let you implement design, say for only the iPhone in landscape, or maybe the iPad in both orientations. You have full control.
For your font related question, you access the size-class control through the '+' sign I mentioned above.
This is a good explanation and tutorial:
Wenderlich size classes
and in Apple's own words:
Apple size classes
EDIT: - If you really want to have different font sizes for different screen sizes, then this answer should help you.
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:
Try this:
Here is (result) preview of font-size variation, in iPhone and iPad
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)
}
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 declare different font sizes for same size class in adaptive auto layout in xcode?
This doesn't work with adaptive layout. Apple thinks, you should use the same font size for these different devices if they have the same size class.
You might have to get the screen size and change the font size according to that.
Different font size on Different devices in same size class
Autolayout
and SizeClasses
wouldn't target specific devices, so you will have to set the font sizes programatically. You can use check the size of your device using UIScreen.mainScreen().bounds.size.height
and set the size of your font accordingly. This solution will clarify you more.
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
}
How to handle different fonts & image sizes for different device resolutions?
Thank you for other fellows.
Earlier, I wasn't aware of 'Minimum Font Scale'
& 'Aspect Ratio'
options in Autolayout.
After some researching, I could accomplish resizing of views and font sizes with them. Here are the few answers which helped me.
https://stackoverflow.com/a/28909109/1083859
https://stackoverflow.com/a/31853203/1083859
https://stackoverflow.com/a/25768875/1083859
Related Topics
Swift - Using Replacerange() to Change Certain Occurrences in a String
CSS Gradient Not Working on iOS
Why Can't I Invert My Image Back to Original with Cifilter in My Swift iOS App
iPhone Simulator Suddenly Started Running Very Slow
Add Shadow on Uiview Using Swift 3
iOS Designated Initializers:Using Ns_Designated_Initializer
Swift Numerics and Cgfloat (Cgpoint, Cgrect, etc.)
Play Video in Background Using Avplayer
Cfbundleversion Must Be Higher Than Previous Version
Swiftui Withanimation Completion Callback
Reverse an Audio File Swift/Objective-C
Uitableviewcells with Uibutton Overlaps While Scrolling
Completion Handler for Uinavigationcontroller "Pushviewcontroller:Animated"
How to Hide Tab Bar with Animation in iOS
Presenting a View Controller Modally from an Action Sheet's Delegate in iOS8 - iOS11
iOS 7 - How to Disable the Swipe Back and Forward Functionality in Safari