Skip to content

Commit e5607eb

Browse files
committed
Use the -boundingRectWithSize:options:attributes:context: method for all string measurement. Fixed issue core-plot#280.
1 parent 2b3ab7a commit e5607eb

9 files changed

+66
-73
lines changed

framework/MacOnly/CPTPlatformSpecificCategories.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,9 @@
3939
-(void)drawInRect:(CGRect)rect inContext:(nonnull CGContextRef)context;
4040
/// @}
4141

42+
/// @name Measurement
43+
/// @{
44+
-(CGSize)sizeAsDrawn;
45+
/// @}
46+
4247
@end

framework/MacOnly/CPTPlatformSpecificCategories.m

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,4 +100,29 @@ -(void)drawInRect:(CGRect)rect inContext:(nonnull CGContextRef)context
100100
CPTPopCGContext();
101101
}
102102

103+
/**
104+
* @brief Computes the size of the styled text when drawn rounded up to the nearest whole number in each dimension.
105+
**/
106+
-(CGSize)sizeAsDrawn
107+
{
108+
CGRect rect = CGRectZero;
109+
110+
if ( [self respondsToSelector:@selector(boundingRectWithSize:options:context:)] ) {
111+
rect = [self boundingRectWithSize:CPTSizeMake(10000.0, 10000.0)
112+
options:CPTStringDrawingOptions
113+
context:nil];
114+
}
115+
else {
116+
rect = [self boundingRectWithSize:CPTSizeMake(10000.0, 10000.0)
117+
options:CPTStringDrawingOptions];
118+
}
119+
120+
CGSize textSize = rect.size;
121+
122+
textSize.width = ceil(textSize.width);
123+
textSize.height = ceil(textSize.height);
124+
125+
return textSize;
126+
}
127+
103128
@end

framework/MacOnly/CPTTextStylePlatformSpecific.m

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -198,25 +198,15 @@ @implementation NSString(CPTTextStyleExtensions)
198198
**/
199199
-(CGSize)sizeWithTextStyle:(nullable CPTTextStyle *)style
200200
{
201-
NSFont *theFont = nil;
202-
NSString *fontName = style.fontName;
203-
204-
if ( fontName ) {
205-
theFont = [NSFont fontWithName:fontName size:style.fontSize];
206-
}
201+
CGRect rect = [self boundingRectWithSize:CPTSizeMake(10000.0, 10000.0)
202+
options:CPTStringDrawingOptions
203+
attributes:style.attributes
204+
context:nil];
207205

208-
CGSize textSize;
206+
CGSize textSize = rect.size;
209207

210-
if ( theFont ) {
211-
CPTDictionary *attributes = @{
212-
NSFontAttributeName: theFont
213-
};
214-
215-
textSize = NSSizeToCGSize([self sizeWithAttributes:attributes]);
216-
}
217-
else {
218-
textSize = CGSizeZero;
219-
}
208+
textSize.width = ceil(textSize.width);
209+
textSize.height = ceil(textSize.height);
220210

221211
return textSize;
222212
}

framework/Source/CPTLegendEntry.m

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -219,12 +219,8 @@ -(CGSize)titleSize
219219

220220
NSAttributedString *styledTitle = self.attributedTitle;
221221

222-
if ( (styledTitle.length > 0) && [styledTitle respondsToSelector:@selector(size)] ) {
223-
#if TARGET_OS_SIMULATOR || TARGET_OS_IPHONE
224-
theTitleSize = styledTitle.size;
225-
#else
226-
theTitleSize = NSSizeToCGSize(styledTitle.size);
227-
#endif
222+
if ( styledTitle.length > 0 ) {
223+
theTitleSize = [styledTitle sizeAsDrawn];
228224
}
229225
else {
230226
NSString *theTitle = styledTitle.string;

framework/Source/CPTTextLayer.m

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -316,12 +316,8 @@ -(CGSize)sizeThatFits
316316

317317
if ( myText.length > 0 ) {
318318
NSAttributedString *styledText = self.attributedText;
319-
if ( (styledText.length > 0) && [styledText respondsToSelector:@selector(size)] ) {
320-
#if TARGET_OS_SIMULATOR || TARGET_OS_IPHONE
321-
textSize = styledText.size;
322-
#else
323-
textSize = NSSizeToCGSize(styledText.size);
324-
#endif
319+
if ( styledText.length > 0 ) {
320+
textSize = [styledText sizeAsDrawn];
325321
}
326322
else {
327323
textSize = [myText sizeWithTextStyle:self.textStyle];

framework/iPhoneOnly/CPTPlatformSpecificCategories.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,9 @@
5353
-(void)drawInRect:(CGRect)rect inContext:(nonnull CGContextRef)context;
5454
/// @}
5555

56+
/// @name Measurement
57+
/// @{
58+
-(CGSize)sizeAsDrawn;
59+
/// @}
60+
5661
@end

framework/iPhoneOnly/CPTPlatformSpecificCategories.m

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,4 +114,21 @@ -(void)drawInRect:(CGRect)rect inContext:(nonnull CGContextRef)context
114114
}
115115
}
116116

117+
/**
118+
* @brief Computes the size of the styled text when drawn rounded up to the nearest whole number in each dimension.
119+
**/
120+
-(CGSize)sizeAsDrawn
121+
{
122+
CGRect rect = [self boundingRectWithSize:CPTSizeMake(10000.0, 10000.0)
123+
options:CPTStringDrawingOptions
124+
context:nil];
125+
126+
CGSize textSize = rect.size;
127+
128+
textSize.width = ceil(textSize.width);
129+
textSize.height = ceil(textSize.height);
130+
131+
return textSize;
132+
}
133+
117134
@end

framework/iPhoneOnly/CPTTextStylePlatformSpecific.h

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,3 @@ typedef NS_ENUM (NSInteger, CPTTextAlignment) {
1010
CPTTextAlignmentJustified = NSTextAlignmentJustified, ///< Justified alignment.
1111
CPTTextAlignmentNatural = NSTextAlignmentNatural ///< Natural alignment of the text's script.
1212
};
13-
14-
// @cond
15-
// for iOS SDK compatibility
16-
#if TARGET_OS_SIMULATOR || TARGET_OS_IPHONE
17-
#if __IPHONE_OS_VERSION_MAX_ALLOWED<70000
18-
@interface NSString(CPTTextStylePlatformSpecificExtensions)
19-
20-
-(CGSize)sizeWithAttributes:(nonnull CPTDictionary *)attrs;
21-
22-
@end
23-
#else
24-
#endif
25-
#endif
26-
27-
/// @endcond

framework/iPhoneOnly/CPTTextStylePlatformSpecific.m

Lines changed: 3 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -169,41 +169,15 @@ @implementation NSString(CPTTextStyleExtensions)
169169
**/
170170
-(CGSize)sizeWithTextStyle:(nullable CPTTextStyle *)style
171171
{
172-
CGSize textSize;
173-
174-
#if TARGET_OS_SIMULATOR || TARGET_OS_TV
175172
CGRect rect = [self boundingRectWithSize:CPTSizeMake(10000.0, 10000.0)
176173
options:CPTStringDrawingOptions
177174
attributes:style.attributes
178175
context:nil];
179-
textSize = rect.size;
180-
textSize.width = ceil(textSize.width);
181-
textSize.height = ceil(textSize.height);
182-
#else
183-
// -boundingRectWithSize:options:attributes:context: is available in iOS 7.0 and later
184-
if ( [self respondsToSelector:@selector(boundingRectWithSize:options:attributes:context:)] ) {
185-
CGRect rect = [self boundingRectWithSize:CPTSizeMake(10000.0, 10000.0)
186-
options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading | NSStringDrawingTruncatesLastVisibleLine
187-
attributes:style.attributes
188-
context:nil];
189-
textSize = rect.size;
190-
textSize.width = ceil(textSize.width);
191-
textSize.height = ceil(textSize.height);
192-
}
193-
else {
194-
UIFont *theFont = nil;
195-
NSString *fontName = style.fontName;
196176

197-
if ( fontName ) {
198-
theFont = [UIFont fontWithName:fontName size:style.fontSize];
199-
}
177+
CGSize textSize = rect.size;
200178

201-
#pragma clang diagnostic push
202-
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
203-
textSize = [self sizeWithFont:theFont constrainedToSize:CPTSizeMake(10000.0, 10000.0)];
204-
#pragma clang diagnostic pop
205-
}
206-
#endif
179+
textSize.width = ceil(textSize.width);
180+
textSize.height = ceil(textSize.height);
207181

208182
return textSize;
209183
}

0 commit comments

Comments
 (0)