TestsTested | ✓ |
LangLanguage | Obj-CObjective C |
License | MIT |
ReleasedLast Release | Jan 2017 |
Maintained by Jonathan Hersh, Samuel Giddins.
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 (iOS 8) or San Francisco (iOS 9). 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.
Xcode 7.0+ with iOS 7.0+ SDK.
SSDynamicText
provides dynamic auto-sizing labels, buttons, text fields, and text views that respond when the user changes her preferred text size. Check out a full example.
UIKit views that responds when the user changes her preferred text size:
SSDynamicLabel
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];
SSDynamicButton
SSDynamicButton *myButton = [SSDynamicButton buttonWithFont:@"Courier"
baseSize:16.0f];
[myButton setText:@"Auto-sizing text!" forControlState:UIControlStateNormal];
SSDynamicTextField
SSDynamicTextField *myTextField = [SSDynamicTextField textFieldWithFont:@"Courier"
baseSize:16.0f];
myTextField.text = @"Auto-sizing text!";
SSDynamicTextView
SSDynamicTextView *myTextView = [SSDynamicTextView textViewWithFont:@"Courier"
baseSize:16.0f];
myTextView.text = @"Auto-sizing text!";
UIFont+SSTextSize
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];
UIApplication+SSTextSize
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];
NSAttributedString
SupportSSDynamicText supports attributed text! Assign your NSAttributedString
to the new dynamicAttributedText
property.
UITextView
and UITextField
sometimes internally call -setAttributedText:
even with normal text. To best accommodate that internal implementation detail, we've added a new dynamicAttribtuedText
property instead of overriding -setAttributedText:
.
@property (nonatomic, copy) NSAttributedString *dynamicAttributedText;
SSDynamicText
is a @jhersh production -- (electronic mail | @jhersh)