Skip to content

Commit 1f1f1d8

Browse files
zilongshanrenminggo
authored andcommitted
fix ios label shrink overflow clamp letters issue (#17737)
1 parent ddb4dc8 commit 1f1f1d8

File tree

1 file changed

+19
-12
lines changed

1 file changed

+19
-12
lines changed

cocos/platform/ios/CCDevice-ios.mm

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ of this software and associated documentation files (the "Software"), to deal
4444
// Vibrate
4545
#import <AudioToolbox/AudioToolbox.h>
4646

47+
const float MAX_MEASURE_HEIGHT = 10000;
48+
49+
4750
static NSAttributedString* __attributedStringWithFontSize(NSMutableAttributedString* attributedString, CGFloat fontSize)
4851
{
4952
{
@@ -82,7 +85,11 @@ static CGFloat _calculateTextDrawStartHeight(cocos2d::Device::TextAlign align, C
8285
return startH;
8386
}
8487

85-
static CGSize _calculateShrinkedSizeForString(NSAttributedString **str, id font, CGSize constrainSize, bool enableWrap, int& newFontSize)
88+
static CGSize _calculateShrinkedSizeForString(NSAttributedString **str,
89+
id font,
90+
CGSize constrainSize,
91+
bool enableWrap,
92+
int& newFontSize)
8693
{
8794
CGRect actualSize = CGRectMake(0, 0, constrainSize.width + 1, constrainSize.height + 1);
8895
int fontSize = [font pointSize];
@@ -102,7 +109,7 @@ static CGSize _calculateShrinkedSizeForString(NSAttributedString **str, id font,
102109
*str = __attributedStringWithFontSize(mutableString, fontSize);
103110

104111
CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString((__bridge CFAttributedStringRef)*str);
105-
CGSize targetSize = CGSizeMake(CGFLOAT_MAX, CGFLOAT_MAX);
112+
CGSize targetSize = CGSizeMake(MAX_MEASURE_HEIGHT, MAX_MEASURE_HEIGHT);
106113
CGSize fitSize = CTFramesetterSuggestFrameSizeWithConstraints(framesetter, CFRangeMake(0, [(*str) length]), NULL, targetSize, NULL);
107114
CFRelease(framesetter);
108115
if (fitSize.width == 0 || fitSize.height == 0) {
@@ -135,10 +142,10 @@ static CGSize _calculateShrinkedSizeForString(NSAttributedString **str, id font,
135142
NSMutableAttributedString *mutableString = [[*str mutableCopy] autorelease];
136143
*str = __attributedStringWithFontSize(mutableString, fontSize);
137144

138-
CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString((__bridge CFAttributedStringRef)*str);
139-
CGSize targetSize = CGSizeMake(constrainSize.width, CGFLOAT_MAX);
140-
CGSize fitSize = CTFramesetterSuggestFrameSizeWithConstraints(framesetter, CFRangeMake(0, [(*str) length]), NULL, targetSize, NULL);
141-
CFRelease(framesetter);
145+
CGSize fitSize = [*str boundingRectWithSize:CGSizeMake(constrainSize.width, MAX_MEASURE_HEIGHT)
146+
options:(NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading)
147+
context:nil].size;
148+
142149
if (fitSize.width == 0 || fitSize.height == 0) {
143150
continue;
144151
}
@@ -160,7 +167,7 @@ static CGSize _calculateShrinkedSizeForString(NSAttributedString **str, id font,
160167

161168
newFontSize = fontSize;
162169

163-
return CGSizeMake(actualSize.size.width, actualSize.size.height);
170+
return CGSizeMake(ceilf(actualSize.size.width), ceilf(actualSize.size.height));
164171
}
165172

166173
#define SENSOR_DELAY_GAME 0.02
@@ -339,16 +346,16 @@ static CGSize _calculateStringSize(NSAttributedString *str, id font, CGSize *con
339346
{
340347
CGSize textRect = CGSizeZero;
341348
textRect.width = constrainSize->width > 0 ? constrainSize->width
342-
: CGFLOAT_MAX;
349+
: MAX_MEASURE_HEIGHT;
343350
textRect.height = constrainSize->height > 0 ? constrainSize->height
344-
: CGFLOAT_MAX;
351+
: MAX_MEASURE_HEIGHT;
345352

346353
if (overflow == 1) {
347354
if(!enableWrap) {
348-
textRect.width = CGFLOAT_MAX;
349-
textRect.height = CGFLOAT_MAX;
355+
textRect.width = MAX_MEASURE_HEIGHT;
356+
textRect.height = MAX_MEASURE_HEIGHT;
350357
} else {
351-
textRect.height = CGFLOAT_MAX;
358+
textRect.height = MAX_MEASURE_HEIGHT;
352359
}
353360
}
354361

0 commit comments

Comments
 (0)