Giter VIP home page Giter VIP logo

ssdynamictext's Introduction

SSDynamicText

Circle CI codecov.io

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

Install with CocoaPods. Add to your Podfile:

pod 'SSDynamicText', :head # YOLO

SSDynamicLabel

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];

SSDynamicButton

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];                                     

SSDynamicTextField

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!";

SSDynamicTextView

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!";

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 Support

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;

Thanks!

SSDynamicText is a @jhersh production -- (electronic mail | @jhersh)

ssdynamictext's People

Contributors

grubas7 avatar jhersh avatar mrgrauel avatar remki avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.