fix ios label shrink overflow clamp letters issue (#17737)

This commit is contained in:
子龙山人 2017-04-25 09:04:00 +08:00 committed by minggo
parent ddb4dc893b
commit 1f1f1d8f1a
1 changed files with 19 additions and 12 deletions

View File

@ -44,6 +44,9 @@
// Vibrate // Vibrate
#import <AudioToolbox/AudioToolbox.h> #import <AudioToolbox/AudioToolbox.h>
const float MAX_MEASURE_HEIGHT = 10000;
static NSAttributedString* __attributedStringWithFontSize(NSMutableAttributedString* attributedString, CGFloat fontSize) static NSAttributedString* __attributedStringWithFontSize(NSMutableAttributedString* attributedString, CGFloat fontSize)
{ {
{ {
@ -82,7 +85,11 @@ static CGFloat _calculateTextDrawStartHeight(cocos2d::Device::TextAlign align, C
return startH; return startH;
} }
static CGSize _calculateShrinkedSizeForString(NSAttributedString **str, id font, CGSize constrainSize, bool enableWrap, int& newFontSize) static CGSize _calculateShrinkedSizeForString(NSAttributedString **str,
id font,
CGSize constrainSize,
bool enableWrap,
int& newFontSize)
{ {
CGRect actualSize = CGRectMake(0, 0, constrainSize.width + 1, constrainSize.height + 1); CGRect actualSize = CGRectMake(0, 0, constrainSize.width + 1, constrainSize.height + 1);
int fontSize = [font pointSize]; int fontSize = [font pointSize];
@ -102,7 +109,7 @@ static CGSize _calculateShrinkedSizeForString(NSAttributedString **str, id font,
*str = __attributedStringWithFontSize(mutableString, fontSize); *str = __attributedStringWithFontSize(mutableString, fontSize);
CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString((__bridge CFAttributedStringRef)*str); CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString((__bridge CFAttributedStringRef)*str);
CGSize targetSize = CGSizeMake(CGFLOAT_MAX, CGFLOAT_MAX); CGSize targetSize = CGSizeMake(MAX_MEASURE_HEIGHT, MAX_MEASURE_HEIGHT);
CGSize fitSize = CTFramesetterSuggestFrameSizeWithConstraints(framesetter, CFRangeMake(0, [(*str) length]), NULL, targetSize, NULL); CGSize fitSize = CTFramesetterSuggestFrameSizeWithConstraints(framesetter, CFRangeMake(0, [(*str) length]), NULL, targetSize, NULL);
CFRelease(framesetter); CFRelease(framesetter);
if (fitSize.width == 0 || fitSize.height == 0) { if (fitSize.width == 0 || fitSize.height == 0) {
@ -135,10 +142,10 @@ static CGSize _calculateShrinkedSizeForString(NSAttributedString **str, id font,
NSMutableAttributedString *mutableString = [[*str mutableCopy] autorelease]; NSMutableAttributedString *mutableString = [[*str mutableCopy] autorelease];
*str = __attributedStringWithFontSize(mutableString, fontSize); *str = __attributedStringWithFontSize(mutableString, fontSize);
CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString((__bridge CFAttributedStringRef)*str); CGSize fitSize = [*str boundingRectWithSize:CGSizeMake(constrainSize.width, MAX_MEASURE_HEIGHT)
CGSize targetSize = CGSizeMake(constrainSize.width, CGFLOAT_MAX); options:(NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading)
CGSize fitSize = CTFramesetterSuggestFrameSizeWithConstraints(framesetter, CFRangeMake(0, [(*str) length]), NULL, targetSize, NULL); context:nil].size;
CFRelease(framesetter);
if (fitSize.width == 0 || fitSize.height == 0) { if (fitSize.width == 0 || fitSize.height == 0) {
continue; continue;
} }
@ -160,7 +167,7 @@ static CGSize _calculateShrinkedSizeForString(NSAttributedString **str, id font,
newFontSize = fontSize; newFontSize = fontSize;
return CGSizeMake(actualSize.size.width, actualSize.size.height); return CGSizeMake(ceilf(actualSize.size.width), ceilf(actualSize.size.height));
} }
#define SENSOR_DELAY_GAME 0.02 #define SENSOR_DELAY_GAME 0.02
@ -339,16 +346,16 @@ static CGSize _calculateStringSize(NSAttributedString *str, id font, CGSize *con
{ {
CGSize textRect = CGSizeZero; CGSize textRect = CGSizeZero;
textRect.width = constrainSize->width > 0 ? constrainSize->width textRect.width = constrainSize->width > 0 ? constrainSize->width
: CGFLOAT_MAX; : MAX_MEASURE_HEIGHT;
textRect.height = constrainSize->height > 0 ? constrainSize->height textRect.height = constrainSize->height > 0 ? constrainSize->height
: CGFLOAT_MAX; : MAX_MEASURE_HEIGHT;
if (overflow == 1) { if (overflow == 1) {
if(!enableWrap) { if(!enableWrap) {
textRect.width = CGFLOAT_MAX; textRect.width = MAX_MEASURE_HEIGHT;
textRect.height = CGFLOAT_MAX; textRect.height = MAX_MEASURE_HEIGHT;
} else { } else {
textRect.height = CGFLOAT_MAX; textRect.height = MAX_MEASURE_HEIGHT;
} }
} }