In iOS 8, we can design a different UI layout for each size class. The issue I'm facing is, I've designed a layout for Compact Width and Regular Height (size class for all iPhones in portrait) but i want to keep font size of labels smaller for 3.5 and 4 inch devices (iPhone 4 and 5), then relatively bigger for 4.7 inch (iPhone 6) and more bigger for 5.5 inch (iPhone 6 Plus) devices. I've searched but unable to find a solution to set different font size for different devices within same size class.
相关问题
- Core Data lightweight migration crashes after App
- How can I implement password recovery in an iPhone
- State preservation and restoration strategies with
- “Zero out” sensitive String data in Swift
- Get the NSRange for the visible text after scroll
相关文章
- 现在使用swift开发ios应用好还是swift?
- UITableView dragging distance with UIRefreshContro
- TCC __TCCAccessRequest_block_invoke
- Where does a host app handle NSExtensionContext#co
- Swift - hide pickerView after value selected
- How do you detect key up / key down events from a
- didBeginContact:(SKPhysicsContact *)contact not in
- Attempt to present UIAlertController on View Contr
I'm handling it in a project in Swift 3+ using a UILabel Custom class, UILabel extension, and UIDevice extension as generic solution.
UIDevice extension to get
screenType
:Following is the UILabel extension that uses
screenType
to adjust font size.adjustsFontSizeToFitDevice
method could be added in UILabel custom class too, but I've put it in UILabel extension to make it accessible from all types of UILabel instances.The constant "2" used in
adjustsFontSizeToFitDevice
method can be changed to any desired number. My logic is to consider iPhone 6/7/8 as default resolution, and give suitable font size (in Storyboard) to each label for that resolution. Then, I'm adding 2 points for iPhone X and iPhone 6/7/8 Plus, whereas subtracting 2 points for iPhone 4/5.Finally a UILabel custom class to apply font adjustment to all labels that are sub-classed from
MyCustomLabel
.Usage: In Storyboard, sub-class all those instances of UILabel from
MyCustomLabel
whose font size needs to be adjusted according to device size.Instead of writing code for each and every label, do just extend your label class with Your custom label class as below and it will automatically scale based on device resolution scaling factor:
You can achieve desired effect as below.
Usage :
instead of using 14 as font size you can use14.fontSize
, it will changed as per device, depends on you delta value.Usage:
UIFont.font_medium(12.fontSize)
UIFont extension:
UIDevice Extension:
Int Extension:
Create like this,
after that where you want to change label font we can write simple Switch case
Two ways:
1)Manually make a method in app delegate, share its object and call method.
eg :
2) On every UI item, go to attributes inspector, declare a font.
(There is a + sign visible to the left of Font size field. Click on it ,select the matching size class and declare font size.)
The second option is convenient for me. Happy Coding!
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: