In a program I was working on, I decided to use Dynamic Type. You can read about it here, but suffice it to say, it is a new way of handling text sizes in iOS7. The user can set a value in their iOS device’s settings to set all text to be bigger or smaller than default, for any app that implements Dynamic Type. This is a bit more advanced than my other posts, but it is surprisingly easy to do, so I thought I would write a post about what I learned.
Setup and using preferredFontForTextStyle
First off, you have to create an outlet for any label, textfield, or whatever you have that you want to dynamically change the text size of, so as to have a place to set the font. The easiest way to do this is to use the assistant editor on your view, and command+drag your text control to the assistant editor (in the @interface area of your .m for a private outlet). Once you release it will ask you a few questions, and the defaults are fine usually, so you just have to give it a name.
I then made a single helper method that I would call to perform all of the appropriate updates. It doesn’t take any arguments, it simply sets all of the fonts for the outlets you want to set. This method is very simple. All you do is set the font property of the outlets we just made. The key to using dynamic type, is the UIFont’s method preferredFontForTextStyle:. This method takes an NSString that denotes the style that you want to set the text to. There are 6 predefined styles that you can use in this method. They are:
- UIFontTextStyleHeadline
- UIFontTextStyleSubheadline
- UIFontTextStyleBody
- UIFontTextStyleFootnote
- UIFontTextStyleCaption1
- UIFontTextStyleCaption2;
So with those, a sample method for this could look like:
-(void) doDynamicTypeThings
{
self.someHeadline.font = [UIFont preferredFontForTextStyle:UIFontTextStyleHeadline];
self.someBodyTextLabel.font = [UIFont preferredFontForTextStyle:UIFontTextStyleBody];
}
Now whenever this is called, you will update the font to whatever dynamic type says it should be.
Using the Dynamic Type Helper Method
I run this method in 2 places. The first is an easy one. It is just in viewWillAppear. So when the view is about to appear, this is called and will set the font appropriately. If you only did it here though, you leave a big hole in the program. If you ran your program at one dynamic type setting, go out to settings, change the dynamic type slider, and went back to your program, it won’t update. To fix this, we have to put doDynamicTypeThings somewhere else.
We need to listen for when these sort of updates happen, so that it will see that there was a change, and rerun our Dynamic Type method. To do that, we have to subscribe to a notification that tells us such, and that one is UIContentSizeCategoryDidChangeNotification. I added the my subscription to this notification to viewWillAppear as well, so my viewWillAppear looks like:
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(preferredContentSizeChanged:)
name:UIContentSizeCategoryDidChangeNotification
object:nil];
[self doDynamicTypeThings];
}
The astute will see that that message to NSNotificationCenter doesn’t actually run my doDynamicTypeThings. That message is sent at the end of viewWillAppear, but that only happens when viewWillAppear runs. We now need to implement the selector specified in our message to NSNotificationCenter. In there we run the actual method we need, so it looks like:
- (void)preferredContentSizeChanged:(NSNotification *)notification
{
[self doDynamicTypeThings];
[self.view setNeedsLayout];
}
So now when we get that notification, we will run this method, which runs our method to change all of the fonts.
Cleaning up NSNotification Center when we are done
I also added the command to unsubscribe to this in viewWillDisappear, so we don’t get notifications if this view is not up, so it looks like:
-(void) viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
In this case though, I actually told NSNotificationCenter to remove my view controller itself as an observer entirely, so this will remove ALL notifications I am subscribed to. There is a way to specify a certain notification, but I felt it was unnecessary here.
Conclusion
While this post might make it look long, we basically did this:
- Created outlets for anything we wanted Dynamic Type to resize.
- Set fonts of this outlets to the appropriate font using preferredFontForTextStyle: in a helper method.
- Ran that helper method in viewWillAppear.
- Subscribed to the UIContentSizeCategoryDidChangeNotification notification.
- Ran the helper method when that notification is received.
- Unsubscribed from all notifications in viewWillDisappear.
That’s about all there is to it. While people probably won’t change this value very often, it is a good idea to update in case they do. So now if they need the text in your app a bit bigger to read, it will update appropriately, without making them have to force quit the app or move back and forth from another view in your app to force viewWillAppear to run again.
I hope this was helpful for implementing Dynamic Type in your apps. If you have any questions, don’t hesitate to contact me on twitter @CodingExplorer , and I’ll see what I can do.
Thanks!