Size is not applicable.
What about if I want the font to be bigger or smaller than?
What should I do?
I thought it depends on the size of UILabel. However, resizing the UILabel won't work.
iOS 7 uses something called dynamic type. Instead of specifying the exact font with its size and everything, you can describe it semantically by saying, for example, that the font is used for headlines, for body text or whatever you wish. The actual font depends on various parameters which can be changed dynamically, one of which is user's preferred font size chosen in 'Preferences/General/Text Size'. You cannot just choose headline's size because that would make the whole concept meaningless. It's not your choice, it's user's choice. You just have to listen to what user chooses and respond accordingly. You can, however, scale the preferred font programmatically. UIFontDescriptor object is used to obtain the current headline size after which fontWithDescriptor:size:
method is used to obtain a new font with the same descriptor, but new scaled size.
@interface SomeViewController ()
@property (weak, nonatomic) IBOutlet UILabel *headlineLabel;
@end
@implementation SomeViewController
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
// We need to setup our fonts whenever controller appears.
// to account for changes that happened while
// controller wasn't on screen.
[self setupFonts];
// Subscribing to UIContentSizeCategoryDidChangeNotification
// to get notified when user chages the preferred size.
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(preferredFontChanged:)
name:UIContentSizeCategoryDidChangeNotification
object:nil];
}
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillAppear:animated];
// Unsubscribe from UIContentSizeCategoryDidChangeNotification.
[[NSNotificationCenter defaultCenter] removeObserver:self
name:UIContentSizeCategoryDidChangeNotification
object:nil];
}
- (void)setupFonts
{
// In this method we setup fonts for all the labels and text views
// by calling 'preferredFontForTextStyle:scale:'.
self.headlineLabel.font = [self preferredFontForTextStyle:UIFontTextStyleHeadline scale:0.8];
}
- (UIFont *)preferredFontForTextStyle:(NSString *)style scale:(CGFloat)scale
{
// We first get prefered font descriptor for provided style.
UIFontDescriptor *currentDescriptor = [UIFontDescriptor preferredFontDescriptorWithTextStyle:style];
// Then we get the default size from the descriptor.
// This size can change between iOS releases.
// and should never be hard-codded.
CGFloat headlineSize = [currentDescriptor pointSize];
// We are calculating new size using the provided scale.
CGFloat scaledHeadlineSize = lrint(headlineSize * scale);
// This method will return a font which matches the given descriptor
// (keeping all the attributes like 'bold' etc. from currentDescriptor),
// but it will use provided size if it's greater than 0.0.
return [UIFont fontWithDescriptor:currentDescriptor size:scaledHeadlineSize];
}
- (void)preferredFontChanged:(NSNotification *)notification
{
[self setupFonts];
}
@end
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With