Skip to content

Commit bda5f09

Browse files
committed
Scaling dynamic text example
1 parent 1fbe84a commit bda5f09

File tree

6 files changed

+83
-51
lines changed

6 files changed

+83
-51
lines changed

DynamicText/DynamicText.xcodeproj/project.pbxproj

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
5393B55717F83CB600AD2292 /* InfoPlist.strings in Resources */ = {isa = PBXBuildFile; fileRef = 5393B55517F83CB600AD2292 /* InfoPlist.strings */; };
2323
5393B55917F83CB600AD2292 /* DynamicTextTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 5393B55817F83CB600AD2292 /* DynamicTextTests.m */; };
2424
5393B56417F83DB000AD2292 /* UYLTextStyleViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 5393B56317F83DB000AD2292 /* UYLTextStyleViewController.m */; };
25-
53B8D3861869EF4C00AA7903 /* UYLFont.m in Sources */ = {isa = PBXBuildFile; fileRef = 53B8D3851869EF4C00AA7903 /* UYLFont.m */; };
25+
53FE524C1871CDB700DE5F24 /* UIFont+UYLScaledFont.m in Sources */ = {isa = PBXBuildFile; fileRef = 53FE524B1871CDB700DE5F24 /* UIFont+UYLScaledFont.m */; };
2626
/* End PBXBuildFile section */
2727

2828
/* Begin PBXContainerItemProxy section */
@@ -58,8 +58,8 @@
5858
5393B55817F83CB600AD2292 /* DynamicTextTests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = DynamicTextTests.m; sourceTree = "<group>"; };
5959
5393B56217F83DB000AD2292 /* UYLTextStyleViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = UYLTextStyleViewController.h; sourceTree = "<group>"; };
6060
5393B56317F83DB000AD2292 /* UYLTextStyleViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = UYLTextStyleViewController.m; sourceTree = "<group>"; };
61-
53B8D3841869EF4C00AA7903 /* UYLFont.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = UYLFont.h; sourceTree = "<group>"; };
62-
53B8D3851869EF4C00AA7903 /* UYLFont.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = UYLFont.m; sourceTree = "<group>"; };
61+
53FE524A1871CDB700DE5F24 /* UIFont+UYLScaledFont.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "UIFont+UYLScaledFont.h"; sourceTree = "<group>"; };
62+
53FE524B1871CDB700DE5F24 /* UIFont+UYLScaledFont.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "UIFont+UYLScaledFont.m"; sourceTree = "<group>"; };
6363
/* End PBXFileReference section */
6464

6565
/* Begin PBXFrameworksBuildPhase section */
@@ -126,8 +126,8 @@
126126
5393B56317F83DB000AD2292 /* UYLTextStyleViewController.m */,
127127
531B32081861EFB6005F2332 /* UYLScaledTextStyleViewController.h */,
128128
531B32091861EFB6005F2332 /* UYLScaledTextStyleViewController.m */,
129-
53B8D3841869EF4C00AA7903 /* UYLFont.h */,
130-
53B8D3851869EF4C00AA7903 /* UYLFont.m */,
129+
53FE524A1871CDB700DE5F24 /* UIFont+UYLScaledFont.h */,
130+
53FE524B1871CDB700DE5F24 /* UIFont+UYLScaledFont.m */,
131131
5393B53F17F83CB600AD2292 /* Main.storyboard */,
132132
5393B54517F83CB600AD2292 /* Images.xcassets */,
133133
5393B53417F83CB600AD2292 /* Supporting Files */,
@@ -263,9 +263,9 @@
263263
buildActionMask = 2147483647;
264264
files = (
265265
5393B53A17F83CB600AD2292 /* main.m in Sources */,
266-
53B8D3861869EF4C00AA7903 /* UYLFont.m in Sources */,
267266
5393B56417F83DB000AD2292 /* UYLTextStyleViewController.m in Sources */,
268267
5393B53E17F83CB600AD2292 /* UYLAppDelegate.m in Sources */,
268+
53FE524C1871CDB700DE5F24 /* UIFont+UYLScaledFont.m in Sources */,
269269
531B320A1861EFB6005F2332 /* UYLScaledTextStyleViewController.m in Sources */,
270270
);
271271
runOnlyForDeploymentPostprocessing = 0;
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
//
2+
// UIFont+UYLScaledFont.h
3+
// DynamicText
4+
//
5+
// Created by Keith Harrison http://useyourloaf.com
6+
// Copyright (c) 2013 Keith Harrison. All rights reserved.
7+
//
8+
// Redistribution and use in source and binary forms, with or without
9+
// modification, are permitted provided that the following conditions are met:
10+
//
11+
// Redistributions of source code must retain the above copyright
12+
// notice, this list of conditions and the following disclaimer.
13+
//
14+
// Redistributions in binary form must reproduce the above copyright
15+
// notice, this list of conditions and the following disclaimer in the
16+
// documentation and/or other materials provided with the distribution.
17+
//
18+
// Neither the name of Keith Harrison nor the names of its contributors
19+
// may be used to endorse or promote products derived from this software
20+
// without specific prior written permission.
21+
//
22+
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER ''AS IS'' AND ANY
23+
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
24+
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
25+
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
26+
// DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
27+
// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28+
// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
29+
// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30+
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31+
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32+
33+
34+
#import <UIKit/UIKit.h>
35+
36+
@interface UIFont (UYLScaledFont)
37+
38+
// +uylPreferredFontForTextStyle:scale:
39+
//
40+
// Return a UIFont object for the specified text style that is scaled by the
41+
// speficied scale factor.
42+
//
43+
// The text style is a UIFontDescriptor Text Style
44+
//
45+
// The scaleFactor modifies the default point size of the text style. So for
46+
// a font that is twice the default size specify a scaleFactor of 2.0.
47+
48+
+ (UIFont *)uylPreferredFontForTextStyle:(NSString *)style scale:(CGFloat)scaleFactor;
49+
50+
@end

DynamicText/DynamicText/UYLFont.m renamed to DynamicText/DynamicText/UIFont+UYLScaledFont.m

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//
2-
// UYLFont.m
2+
// UIFont+UYLScaledFont.m
33
// DynamicText
44
//
55
// Created by Keith Harrison http://useyourloaf.com
@@ -30,21 +30,14 @@
3030
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
3131
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3232

33-
#import "UYLFont.h"
3433

35-
@implementation UYLFont
34+
#import "UIFont+UYLScaledFont.h"
3635

37-
+ (UIFont *)preferredFontForTextStyle:(NSString *)style scale:(CGFloat)scaleFactor
36+
@implementation UIFont (UYLScaledFont)
37+
38+
+ (UIFont *)uylPreferredFontForTextStyle:(NSString *)style scale:(CGFloat)scaleFactor
3839
{
3940
UIFontDescriptor *descriptor = [UIFontDescriptor preferredFontDescriptorWithTextStyle:style];
40-
41-
// Changing the font family or face of the descriptor has no effect when a text
42-
// style has been set. Can only change symbolic traits.
43-
// UIFontDescriptor *myDescriptor = [defaultDescriptor fontDescriptorWithFamily:@"Verdana"];
44-
45-
// Example of changing the symbolic traits of the descriptor
46-
// UIFontDescriptor *myDescriptor = [defaultDescriptor fontDescriptorWithSymbolicTraits:UIFontDescriptorTraitItalic];
47-
4841
CGFloat pointSize = descriptor.pointSize * scaleFactor;
4942
UIFont *font = [UIFont fontWithDescriptor:descriptor size:pointSize];
5043
return font;

DynamicText/DynamicText/UYLFont.h

Lines changed: 0 additions & 25 deletions
This file was deleted.

DynamicText/DynamicText/UYLScaledTextStyleViewController.m

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
//
88

99
#import "UYLScaledTextStyleViewController.h"
10-
#import "UYLFont.h"
10+
#import "UIFont+UYLScaledFont.h"
1111

1212
@implementation UYLScaledTextStyleViewController
1313

@@ -19,12 +19,12 @@ - (void)configureView
1919
{
2020
CGFloat scaleFactor = 2.0;
2121
self.textSizeLabel.text = [[UIApplication sharedApplication] preferredContentSizeCategory];
22-
self.headlineLabel.font = [UYLFont preferredFontForTextStyle:UIFontTextStyleHeadline scale:scaleFactor];
23-
self.subheadLabel.font = [UYLFont preferredFontForTextStyle:UIFontTextStyleSubheadline scale:scaleFactor];
24-
self.bodyLabel.font = [UYLFont preferredFontForTextStyle:UIFontTextStyleBody scale:scaleFactor];
25-
self.caption1Label.font = [UYLFont preferredFontForTextStyle:UIFontTextStyleCaption1 scale:scaleFactor];
26-
self.caption2Label.font = [UYLFont preferredFontForTextStyle:UIFontTextStyleCaption2 scale:scaleFactor];
27-
self.footnoteLabel.font = [UYLFont preferredFontForTextStyle:UIFontTextStyleFootnote scale:scaleFactor];
22+
self.headlineLabel.font = [UIFont uylPreferredFontForTextStyle:UIFontTextStyleHeadline scale:scaleFactor];
23+
self.subheadLabel.font = [UIFont uylPreferredFontForTextStyle:UIFontTextStyleSubheadline scale:scaleFactor];
24+
self.bodyLabel.font = [UIFont uylPreferredFontForTextStyle:UIFontTextStyleBody scale:scaleFactor];
25+
self.caption1Label.font = [UIFont uylPreferredFontForTextStyle:UIFontTextStyleCaption1 scale:scaleFactor];
26+
self.caption2Label.font = [UIFont uylPreferredFontForTextStyle:UIFontTextStyleCaption2 scale:scaleFactor];
27+
self.footnoteLabel.font = [UIFont uylPreferredFontForTextStyle:UIFontTextStyleFootnote scale:scaleFactor];
2828
}
2929

3030
@end

DynamicText/README

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,25 @@
22
DynamicText - Responding to text size changes
33

44
Version 1.0 06 October 2013 Initial version.
5+
Version 1.1 30 December 2013 Added UYLScaledTextViewController
56
=======================================================================
67

78
This is a example project showing how to respond to the user changing
89
the preferred text size.
910

10-
For further details see the following blog post:
1111

12+
The UYLTextStyleViewController shows the standard way to listen for
13+
and respond to changes to the users preferred text size using the
14+
built-in UIFont Text Styles.
15+
16+
For further details see the following blog post:
1217
http://useyourloaf.com/blog/2013/12/17/supporting-dynamic-type.html
18+
19+
20+
The UYLScaledTextStyleViewController extends the basic approach by
21+
showing how to scale the preferred fonts using a UIFontDescriptor.
22+
A convenience method to return the scaled font is added as a
23+
category of UIFont for this purpose.
24+
25+
For further details see the following blog post:
26+
http://useyourloaf.com/blog/2013/12/30/scaling-dynamic-type-with-font-descriptors.html

0 commit comments

Comments
 (0)