From 1f1f1d8f1af02488890d4c115af5007d96c13ec5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=AD=90=E9=BE=99=E5=B1=B1=E4=BA=BA?= Date: Tue, 25 Apr 2017 09:04:00 +0800 Subject: [PATCH] fix ios label shrink overflow clamp letters issue (#17737) --- cocos/platform/ios/CCDevice-ios.mm | 31 ++++++++++++++++++------------ 1 file changed, 19 insertions(+), 12 deletions(-) diff --git a/cocos/platform/ios/CCDevice-ios.mm b/cocos/platform/ios/CCDevice-ios.mm index 3c5fd32501..b1ac031039 100644 --- a/cocos/platform/ios/CCDevice-ios.mm +++ b/cocos/platform/ios/CCDevice-ios.mm @@ -44,6 +44,9 @@ // Vibrate #import +const float MAX_MEASURE_HEIGHT = 10000; + + static NSAttributedString* __attributedStringWithFontSize(NSMutableAttributedString* attributedString, CGFloat fontSize) { { @@ -82,7 +85,11 @@ static CGFloat _calculateTextDrawStartHeight(cocos2d::Device::TextAlign align, C 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); int fontSize = [font pointSize]; @@ -102,7 +109,7 @@ static CGSize _calculateShrinkedSizeForString(NSAttributedString **str, id font, *str = __attributedStringWithFontSize(mutableString, fontSize); 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); CFRelease(framesetter); if (fitSize.width == 0 || fitSize.height == 0) { @@ -135,10 +142,10 @@ static CGSize _calculateShrinkedSizeForString(NSAttributedString **str, id font, NSMutableAttributedString *mutableString = [[*str mutableCopy] autorelease]; *str = __attributedStringWithFontSize(mutableString, fontSize); - CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString((__bridge CFAttributedStringRef)*str); - CGSize targetSize = CGSizeMake(constrainSize.width, CGFLOAT_MAX); - CGSize fitSize = CTFramesetterSuggestFrameSizeWithConstraints(framesetter, CFRangeMake(0, [(*str) length]), NULL, targetSize, NULL); - CFRelease(framesetter); + CGSize fitSize = [*str boundingRectWithSize:CGSizeMake(constrainSize.width, MAX_MEASURE_HEIGHT) + options:(NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading) + context:nil].size; + if (fitSize.width == 0 || fitSize.height == 0) { continue; } @@ -160,7 +167,7 @@ static CGSize _calculateShrinkedSizeForString(NSAttributedString **str, id font, 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 @@ -339,16 +346,16 @@ static CGSize _calculateStringSize(NSAttributedString *str, id font, CGSize *con { CGSize textRect = CGSizeZero; textRect.width = constrainSize->width > 0 ? constrainSize->width - : CGFLOAT_MAX; + : MAX_MEASURE_HEIGHT; textRect.height = constrainSize->height > 0 ? constrainSize->height - : CGFLOAT_MAX; + : MAX_MEASURE_HEIGHT; if (overflow == 1) { if(!enableWrap) { - textRect.width = CGFLOAT_MAX; - textRect.height = CGFLOAT_MAX; + textRect.width = MAX_MEASURE_HEIGHT; + textRect.height = MAX_MEASURE_HEIGHT; } else { - textRect.height = CGFLOAT_MAX; + textRect.height = MAX_MEASURE_HEIGHT; } }