iOS 7's UIFontDescriptor
is pretty neat. Also pretty neat is dynamic text that responds to the preferred text size that the user specified in Settings.app.
What's not so neat, though, is that +[UIFont preferredFontForTextStyle:]
only works with the system font, Helvetica Neue. What if you have custom fonts and want to respect the user's text size preference?
SSDynamicText is a collection of simple UILabel
, UIButton
, UITextField
, and UITextView
subclasses inspired by this SO answer.
Install with CocoaPods. Add to your Podfile
:
pod 'SSDynamicText', :head # YOLO
A label that responds when the user changes her preferred text size. Check out Example
for a full example.
/**
* Create a dynamic auto-sizing label using a custom font and a base font size.
* The font size will be adjusted up (or down) based on the user's preferred size.
* If the user leaves the app, switches to Settings, and changes preferred size,
* the label will automatically update its size when the app returns to foreground.
*/
SSDynamicLabel *myLabel = [SSDynamicLabel labelWithFont:@"Courier"
baseSize:16.0f];
myLabel.text = @"Auto-sizing text!";
// Already have a font descriptor?
UIFontDescriptor *aDescriptor = [UIFontDescriptor fontDescriptorWithName:@"Courier"
size:16.0f];
SSDynamicLabel *otherLabel = [SSDynamicLabel labelWithFontDescriptor:aDescriptor];
A button with a title label that responds when the user changes her preferred text size. Check out Example
for a full example.
/**
* Create a dynamic auto-sizing button using a custom font and a base font size.
* The font size will be adjusted up (or down) based on the user's preferred size.
* If the user leaves the app, switches to Settings, and changes preferred size,
* the button will automatically update its size when the app returns to foreground.
*/
SSDynamicButton *myButton = [SSDynamicButton buttonWithFont:@"Courier"
baseSize:16.0f];
[myButton setText:@"Auto-sizing text!" forControlState:UIControlStateNormal];
A text field that responds when the user changes her preferred text size. Check out Example
for a full example.
/**
* Create a dynamic auto-sizing text field using a custom font and a base font size.
* The font size will be adjusted up (or down) based on the user's preferred size.
* If the user leaves the app, switches to Settings, and changes preferred size,
* the text field will automatically update its size when the app returns to foreground.
*/
SSDynamicTextField *myTextField = [SSDynamicTextField textFieldWithFont:@"Courier"
baseSize:16.0f];
myTextField.text = @"Auto-sizing text!";
A text view that responds when the user changes her preferred text size. Check out Example
for a full example.
/**
* Create a dynamic auto-sizing text view using a custom font and a base font size.
* The font size will be adjusted up (or down) based on the user's preferred size.
* If the user leaves the app, switches to Settings, and changes preferred size,
* the text view will automatically update its size when the app returns to foreground.
*/
SSDynamicTextView *myTextView = [SSDynamicTextView textViewWithFont:@"Courier"
baseSize:16.0f];
myTextView.text = @"Auto-sizing text!";
/**
* Create a UIFont object using the specified font name and base size.
* The actual size of the returned font is adjusted by
* the user's current preferred font size (specified in Settings.app).
*/
UIFont *myFont = [UIFont dynamicFontWithName:@"Courier" baseSize:16.0f];
/**
* This property returns a numeric delta between the default size setting (Large)
* and the user's current preferred text size.
*
* You probably don't need to use this directly.
*/
NSInteger textDelta = [[UIApplication sharedApplication] preferredFontSizeDelta];
SSDynamicText supports attributed text, all you have to do is set your attributed text to new property dynamicAttributedText.
/*
TextView and TextField sometimes calls setAttributedText even when we work with normal Text.
Framework is using it under the hood sometimes after layouts or even setText calls it. Because of that we cannot override
default attributeText setter to change font, sometimes it change font at random.
*/
@property (nonatomic, copy) NSAttributedString *dynamicAttributedText;
SSDynamicText
is a @jhersh production -- (electronic mail | @jhersh)