iOS7 introduced Dynamic Types. This technology allows apps to dynamically adjust their text content based on user's text size preferences. This is a short presentation on Dynamic Type.
1. Dynamic Type in iOS
Priya Rajagopal
www.priyaontech.com
Twitter: @rajagp
CocoaHeads Ann Arbor
May, 2014
2. Fonts
! Text content represented using fonts (UIFont)
! Fonts = glyphs that share typeface, typestyle and size
! Typeface : Helvetica-Neue, Arial, Times…
! TypeStyle : bold, normal, italic…
! System Fonts
+ (UIFont *)systemFontOfSize:(CGFloat)fontSize;
+ (UIFont *)boldSystemFontOfSize:(CGFloat)fontSize;
+ (UIFont *)italicSystemFontOfSize:(CGFloat)fontSize;
! Custom
+ (UIFont *)fontWithName:(NSString *)fontName size:(CGFloat)fontSize;
! Text Styles …
www.priyaontech.com 2
3. Text Styles
! Introduced in iOS7
! UITextKit
! Describe intended use of text – Headings, SubHeadings,Body
! Every text style associated with a font
+ (UIFont *)preferredFontForTextStyle:(NSString *)style
www.priyaontech.com 3
4. Text Style
Constants
• UIFontTextStyleHeadline
• UIFontTextStyleSubheadline
• UIFontTextStyleBody
• UIFontTextStyleFootnote
• UIFontTextStyleCaption1
• UIFontTextStyleCaption2
www.priyaontech.com 4
Text Styles are implemented
using Dynamic Type …
5. What is Dynamic Type?
! Technology introduced in iOS7
! Users adjust text size preferences
! Settings
! Accessibility
! Apps react by dynamically adjust displayed text content
! Enhanced Readability
! Customized to user’s preferences
www.priyaontech.com 5
7. TextStyles & Dynamic Type
! Fonts associated with text styles change dynamically based
on user preferences
! To support dynamic type, use TextStyles
+ (UIFont *)preferredFontForTextStyle:(NSString *)style
www.priyaontech.com 7
9. Font Descriptors
! Introduced in iOS7 (UIFontDescriptor)
! Describe fonts
! Dictionary of font attributes
UIFontDescriptorTraitsAttribute, UIFontDescriptorFamilyAttribute,
UIFontDescriptorTextStyleAttribute, UIFontDescriptorNameAttribute,
UIFontDescriptorFaceAttribute, UIFontDescriptorSizeAttribute …
! Get/Create font from descriptor
+ (UIFont *)fontWithDescriptor:(UIFontDescriptor *)descriptor size:
(CGFloat)pointSize
! Get descriptor for font
- (UIFontDescriptor *)fontDescriptor
www.priyaontech.com 9
10. Your own Text Style
(with Dynamic Type Support)
! Get font descriptor for an existing TextStyle
UIFontDescriptor* fontDesc = [UIFontDescriptor
preferredFontDescriptorWithTextStyle:UIFontTextStyleHeadline];
! Update descriptor.
UIFontDescriptor* updatedFontDesc = [fontDesc
fontDescriptorByAddingAttributes:@{UIFontDescriptorTraitsAttribute:@{UIFontS
ymbolicTrait:[NSNumber numberWithInteger:UIFontDescriptorTraitItalic]}}];
! Create new font from updated font descriptor
UIFont* myFont = [UIFont fontWithDescriptor:updatedFontDesc
size:fontDesc.pointSize* scale];
* You can’t change the font family. It will be the system font type- Helvetica-Neue.
www.priyaontech.com 10
11. Detecting Text Size Changes
! UIContentSizeCategoryDidChangeNotification
! Register for notification with NSNotificationCenter
! React by invalidating existing fonts and getting new ones
! Adjust container bounds to account for new content size
! Auto layout
! invalidateIntrinsicContentSize/ sizeToFit
www.priyaontech.com 11