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 ;
0 commit comments