Skip to content

Commit 018652e

Browse files
committed
iOS13 UIFont major change as using font name to create a font is deprecated.
1 parent 9c2edae commit 018652e

6 files changed

Lines changed: 68 additions & 9 deletions

File tree

‎framework/MacOnly/CPTTextStylePlatformSpecific.m‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ +(nonnull instancetype)textStyleWithAttributes:(nullable CPTDictionary *)attribu
4040
NSFont *styleFont = attributes[NSFontAttributeName];
4141

4242
if ( styleFont ) {
43+
newStyle.font = styleFont;
4344
newStyle.fontName = styleFont.fontName;
4445
newStyle.fontSize = styleFont.pointSize;
4546
}
@@ -87,10 +88,10 @@ -(nonnull CPTDictionary *)attributes
8788
CPTMutableDictionary *myAttributes = [NSMutableDictionary dictionary];
8889

8990
// Font
90-
NSFont *styleFont = nil;
91+
NSFont *styleFont = self.font;
9192
NSString *fontName = self.fontName;
9293

93-
if ( fontName ) {
94+
if ( styleFont == nil && fontName ) {
9495
styleFont = [NSFont fontWithName:fontName size:self.fontSize];
9596
}
9697

@@ -146,6 +147,7 @@ +(nonnull instancetype)textStyleWithAttributes:(nullable CPTDictionary *)attribu
146147
NSFont *styleFont = attributes[NSFontAttributeName];
147148

148149
if ( styleFont ) {
150+
newStyle.font = styleFont;
149151
newStyle.fontName = styleFont.fontName;
150152
newStyle.fontSize = styleFont.pointSize;
151153
}
@@ -241,10 +243,10 @@ -(void)drawInRect:(CGRect)rect withTextStyle:(nullable CPTTextStyle *)style inCo
241243

242244
CPTPushCGContext(context);
243245

244-
NSFont *theFont = nil;
246+
NSFont *theFont = style.font;
245247
NSString *fontName = style.fontName;
246248

247-
if ( fontName ) {
249+
if ( theFont == nil && fontName ) {
248250
theFont = [NSFont fontWithName:fontName size:style.fontSize];
249251
}
250252

‎framework/Source/CPTMutableTextStyle.h‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@
44

55
@interface CPTMutableTextStyle : CPTTextStyle
66

7+
#if TARGET_OS_SIMULATOR || TARGET_OS_IPHONE || TARGET_OS_TV
8+
@property (readwrite, strong, nonatomic, nullable) UIFont *font;
9+
#else
10+
@property (readwrite, strong, nonatomic, nullable) NSFont *font;
11+
#endif
712
@property (readwrite, copy, nonatomic, nullable) NSString *fontName;
813
@property (readwrite, assign, nonatomic) CGFloat fontSize;
914
@property (readwrite, copy, nonatomic, nullable) CPTColor *color;

‎framework/Source/CPTMutableTextStyle.m‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@
77

88
@implementation CPTMutableTextStyle
99

10+
/** @property UIFont* or NSFont* font
11+
* @brief The font. Default is nil
12+
**/
13+
@synthesize font;
14+
1015
/** @property CGFloat fontSize
1116
* @brief The font size. Default is @num{12.0}.
1217
**/

‎framework/Source/CPTTextStyle.h‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@ typedef NSMutableArray<CPTTextStyle *> CPTMutableTextStyleArray;
1616

1717
@interface CPTTextStyle : NSObject<NSCopying, NSMutableCopying, NSCoding, NSSecureCoding>
1818

19+
// font would override fontName/fontSize if not nil
20+
#if TARGET_OS_SIMULATOR || TARGET_OS_IPHONE || TARGET_OS_TV
21+
@property (readonly, strong, nonatomic, nullable) UIFont *font;
22+
#else
23+
@property (readonly, strong, nonatomic, nullable) NSFont *font;
24+
#endif
1925
@property (readonly, copy, nonatomic, nullable) NSString *fontName;
2026
@property (readonly, nonatomic) CGFloat fontSize;
2127
@property (readonly, copy, nonatomic, nullable) CPTColor *color;

‎framework/Source/CPTTextStyle.m‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@
77
/// @cond
88
@interface CPTTextStyle()
99

10+
// font would override fontName/fontSize if not nil
11+
#if TARGET_OS_SIMULATOR || TARGET_OS_IPHONE || TARGET_OS_TV
12+
@property (readwrite, strong, nonatomic, nullable) UIFont *font;
13+
#else
14+
@property (readwrite, strong, nonatomic, nullable) NSFont *font;
15+
#endif
1016
@property (readwrite, copy, nonatomic, nullable) NSString *fontName;
1117
@property (readwrite, assign, nonatomic) CGFloat fontSize;
1218
@property (readwrite, copy, nonatomic, nullable) CPTColor *color;
@@ -26,6 +32,11 @@ @interface CPTTextStyle()
2632

2733
@implementation CPTTextStyle
2834

35+
/** @property UIFont* or NSFont* font
36+
* @brief The font. Default is nil
37+
**/
38+
@synthesize font;
39+
2940
/** @property CGFloat fontSize
3041
* @brief The font size. Default is @num{12.0}.
3142
**/
@@ -73,6 +84,7 @@ +(nonnull instancetype)textStyleWithStyle:(nullable CPTTextStyle *)textStyle
7384
{
7485
CPTTextStyle *newTextStyle = [[self alloc] init];
7586

87+
newTextStyle.font = textStyle.font;
7688
newTextStyle.color = textStyle.color;
7789
newTextStyle.fontName = textStyle.fontName;
7890
newTextStyle.fontSize = textStyle.fontSize;
@@ -91,6 +103,7 @@ +(nonnull instancetype)textStyleWithStyle:(nullable CPTTextStyle *)textStyle
91103
/** @brief Initializes a newly allocated CPTTextStyle object.
92104
*
93105
* The initialized object will have the following properties:
106+
* - @ref font = nil
94107
* - @ref fontName = Helvetica
95108
* - @ref fontSize = @num{12.0}
96109
* - @ref color = opaque black
@@ -102,6 +115,7 @@ +(nonnull instancetype)textStyleWithStyle:(nullable CPTTextStyle *)textStyle
102115
-(nonnull instancetype)init
103116
{
104117
if ((self = [super init])) {
118+
font = nil;
105119
fontName = @"Helvetica";
106120
fontSize = CPTFloat(12.0);
107121
color = [CPTColor blackColor];
@@ -120,6 +134,16 @@ -(nonnull instancetype)init
120134

121135
-(void)encodeWithCoder:(nonnull NSCoder *)coder
122136
{
137+
#if TARGET_OS_SIMULATOR || TARGET_OS_IPHONE || TARGET_OS_TV
138+
if(self.font) {
139+
//UIFont does not support NSCoding :(
140+
[coder encodeObject:[self.font fontDescriptor] forKey:@"CPTTextStyle.font+descriptor"];
141+
}
142+
#else
143+
// NSFont supports NSCoding :)
144+
[coder encodeObject:self.font forKey:@"CPTTextStyle.font"];
145+
#endif
146+
123147
[coder encodeObject:self.fontName forKey:@"CPTTextStyle.fontName"];
124148
[coder encodeCGFloat:self.fontSize forKey:@"CPTTextStyle.fontSize"];
125149
[coder encodeObject:self.color forKey:@"CPTTextStyle.color"];
@@ -130,6 +154,16 @@ -(void)encodeWithCoder:(nonnull NSCoder *)coder
130154
-(nullable instancetype)initWithCoder:(nonnull NSCoder *)coder
131155
{
132156
if ((self = [super init])) {
157+
158+
#if TARGET_OS_SIMULATOR || TARGET_OS_IPHONE || TARGET_OS_TV
159+
//UIFont does not support NSCoding :(
160+
UIFontDescriptor *fontDescriptor = [coder decodeObjectOfClass:[UIFontDescriptor class] forKey:@"CPTTextStyle.font+descriptor"];
161+
font = [UIFont fontWithDescriptor:fontDescriptor size:0]; // 0 will keep the same font size
162+
#else
163+
// NSFont supports NSCoding :)
164+
font = [coder decodeObjectOfClass:[NSFont class] forKey:@"CPTTextStyle.font"];
165+
#endif
166+
133167
fontName = [[coder decodeObjectOfClass:[NSString class]
134168
forKey:@"CPTTextStyle.fontName"] copy];
135169
fontSize = [coder decodeCGFloatForKey:@"CPTTextStyle.fontSize"];
@@ -164,6 +198,7 @@ -(nonnull id)copyWithZone:(nullable NSZone *)zone
164198
{
165199
CPTTextStyle *newCopy = [[CPTTextStyle allocWithZone:zone] init];
166200

201+
newCopy.font = self.font;
167202
newCopy.fontName = self.fontName;
168203
newCopy.color = self.color;
169204
newCopy.fontSize = self.fontSize;
@@ -184,6 +219,7 @@ -(nonnull id)mutableCopyWithZone:(nullable NSZone *)zone
184219
{
185220
CPTTextStyle *newCopy = [[CPTMutableTextStyle allocWithZone:zone] init];
186221

222+
newCopy.font = self.font;
187223
newCopy.fontName = self.fontName;
188224
newCopy.color = self.color;
189225
newCopy.fontSize = self.fontSize;

‎framework/iPhoneOnly/CPTTextStylePlatformSpecific.m‎

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ +(nonnull instancetype)textStyleWithAttributes:(nullable CPTDictionary *)attribu
4242
UIFont *styleFont = attributes[NSFontAttributeName];
4343

4444
if ( styleFont ) {
45+
newStyle.font = styleFont;
4546
newStyle.fontName = styleFont.fontName;
4647
newStyle.fontSize = styleFont.pointSize;
4748
}
@@ -72,10 +73,10 @@ -(nonnull CPTDictionary *)attributes
7273
CPTMutableDictionary *myAttributes = [NSMutableDictionary dictionary];
7374

7475
// Font
75-
UIFont *styleFont = nil;
76+
UIFont *styleFont = self.font;
7677
NSString *fontName = self.fontName;
7778

78-
if ( fontName ) {
79+
if ( styleFont == nil && fontName ) {
7980
styleFont = [UIFont fontWithName:fontName size:self.fontSize];
8081
}
8182

@@ -132,6 +133,7 @@ +(nonnull instancetype)textStyleWithAttributes:(nullable CPTDictionary *)attribu
132133
UIFont *styleFont = attributes[NSFontAttributeName];
133134

134135
if ( styleFont ) {
136+
newStyle.font = styleFont;
135137
newStyle.fontName = styleFont.fontName;
136138
newStyle.fontSize = styleFont.pointSize;
137139
}
@@ -222,10 +224,10 @@ -(void)drawInRect:(CGRect)rect withTextStyle:(nullable CPTTextStyle *)style inCo
222224
UIColor *styleColor = style.attributes[NSForegroundColorAttributeName];
223225
[styleColor set];
224226

225-
UIFont *theFont = nil;
227+
UIFont *theFont = style.font;
226228
NSString *fontName = style.fontName;
227229

228-
if ( fontName ) {
230+
if (theFont == nil && fontName ) {
229231
theFont = [UIFont fontWithName:fontName size:style.fontSize];
230232
}
231233

@@ -241,7 +243,10 @@ -(void)drawInRect:(CGRect)rect withTextStyle:(nullable CPTTextStyle *)style inCo
241243
UIColor *styleColor = style.attributes[NSForegroundColorAttributeName];
242244
[styleColor set];
243245

244-
UIFont *theFont = [UIFont fontWithName:style.fontName size:style.fontSize];
246+
UIFont *theFont = self.font;
247+
if (theFont == nil) {
248+
theFont = [UIFont fontWithName:style.fontName size:style.fontSize];
249+
}
245250

246251
[self drawInRect:rect
247252
withFont:theFont

0 commit comments

Comments
 (0)