4

I would definitely like to use dynamic type as much as possible within an iOS app - I like the idea that the user can choose whatever size they prefer.

However, when the font size is set to some of the lower values, the app ends up looking frankly ridiculous, with tiny text at one side and a whole load of white space. Personally, I have my phone set to the smallest font (it seems perfect for messages and emails) but would beyond balk at the layout that leads to in this app. (Because of the fixed size of an image it is not possible to fix this layout for smaller fonts.)

In short, I'm looking for a way to set a minimum font size for Body text, or for a specific label regardless of what dynamic type says, or a way to block certain dynamic type levels.

1
  • You could assign a property or tag to the objects that you want to conform to a dynamic font size and check that value to decide which objects will use the dynamic font size Commented Jan 19, 2017 at 18:16

4 Answers 4

1

Try this to set minimum font size for label supporting dynamic type.

In Viewdidload method -

NotificationCenter.default.addObserver(self, selector: #selector(self.handleDynamicTypeChange), name: NSNotification.Name.UIContentSizeCategoryDidChange, object: nil)

In handler method -

func handleDynamicTypeChange() {
    print("size category changed to --->\(UIApplication.shared.preferredContentSizeCategory)")

    if (UIApplication.shared.preferredContentSizeCategory) == UIContentSizeCategory(rawValue: "UICTContentSizeCategoryXS") || (UIApplication.shared.preferredContentSizeCategory) == UIContentSizeCategory(rawValue: "UICTContentSizeCategoryS") {
        dynamicLabel?.font.withSize(12.0)
    }
    else {
        dynamicLabel?.font = UIFont.preferredFont(forTextStyle: .body)
    }
}
Sign up to request clarification or add additional context in comments.

Comments

1

In SwiftUI (iOS 14.0)

To avoid very small font sizes using dynamic type and accessibility text features, there is the minimumScaleFactor modifier which works like this:

struct ContentView: View {
   
    var body: some View {
       Text("Hello World")
         .minimumScaleFactor(0.5)
    }
}

This will avoid scaling your text too much when using dynamic type. In this example the text should not get smaller than half its usual size, if using the value 0.5 in the minimumScaleFactor modifier.

Comments

0

The Dynamic Type feature works with the text styles (11 possible choices since iOS 11) used to emphasize the importance of some legible content or simply to structure the presentation.

The type size values suggested by Apple may be customized but their size variation from one specific type size to another should remain and handled by the system in my view.

The problem to set a minimum font size is that it mustn't be the same for each text style by definition of the text style purpose. Setting a minimum font size means doing it for every text style by defining only the default one (the LARGE size) that will automatically provide the minimum size available according to the variations explanation above.

However, if you do want to impose a minimum size, follow the solution of @KrishnaDattShukla (adding text style differentiation should be implemented as well in this case) or customize your font to let the system work. Beware of the new font sizes values so as not to get a less important text style bigger than a more important one at a certain text size... for instance, the Caption1 text style has a size range of about 3.9×, while LargeTitle is only 1.8× !!!

Comments

0

Instead of adding a notification like @Krishna Datt Shukla uses, you can also override traitCollectionDidChange in a View or ViewController:

override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
   if traitCollection.preferredContentSizeCategory < .small {
       dynamicLabel.font = dynamicLabel.font.withSize(15)
    } else {
       // setup your font again
    }
}

Note that Accessibility Inspector is misleading regarding these notifications, so it is better to test in a device using the control center or using the simulator settings.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.