From 528a769cf6ff3c3561aa7e7bdb07d7cbfc3a0b71 Mon Sep 17 00:00:00 2001 From: minggo Date: Fri, 2 Mar 2012 11:15:44 +0800 Subject: [PATCH] fixed #762: make label display on center on iOS --- cocos2dx/platform/ios/CCImage_ios.mm | 33 +++++-------------- .../ios/FontLabel/FontLabelStringDrawing.h | 1 + .../ios/FontLabel/FontLabelStringDrawing.m | 7 ++++ 3 files changed, 17 insertions(+), 24 deletions(-) diff --git a/cocos2dx/platform/ios/CCImage_ios.mm b/cocos2dx/platform/ios/CCImage_ios.mm index 1aaa0821fc..590e95139f 100644 --- a/cocos2dx/platform/ios/CCImage_ios.mm +++ b/cocos2dx/platform/ios/CCImage_ios.mm @@ -332,17 +332,23 @@ static CGSize _caculateStringSizeWithFontOrZFont(NSString *str, id font, CGSize { NSArray *listItems = [str componentsSeparatedByString: @"\n"]; CGSize dim = CGSizeZero; + CGSize textRect = CGSizeZero; + textRect.width = constrainSize->width > 0 ? constrainSize->width + : 0x7fffffff; + textRect.height = constrainSize->height > 0 ? constrainSize->height + : 0x7fffffff; + for (NSString *s in listItems) { CGSize tmp; if (isZfont) { - tmp = [FontLabelStringDrawingHelper sizeWithZFont:str zfont:font]; + tmp = [FontLabelStringDrawingHelper sizeWithZFont:str zfont:font constrainedToSize:textRect]; } else { - tmp = [s sizeWithFont:font]; + tmp = [s sizeWithFont:font constrainedToSize:textRect]; } if (tmp.width > dim.width) @@ -350,28 +356,7 @@ static CGSize _caculateStringSizeWithFontOrZFont(NSString *str, id font, CGSize dim.width = tmp.width; } - // Should break the string into more lines, so should add the height - if (constrainSize->width > 0 && constrainSize->width < tmp.width) - { - int lines = ceil(tmp.width / constrainSize->width); - dim.height += tmp.height * lines; - } - else - { - dim.height += tmp.height; - } - } - - // Should not exceed the height - if (constrainSize->height > 0) - { - dim.height = constrainSize->height; - } - - // Should not exceed the width; - if (constrainSize->width > 0) - { - dim.width = constrainSize->width; + dim.height += tmp.height; } return dim; diff --git a/cocos2dx/platform/ios/FontLabel/FontLabelStringDrawing.h b/cocos2dx/platform/ios/FontLabel/FontLabelStringDrawing.h index 65b830e5b5..09be868827 100644 --- a/cocos2dx/platform/ios/FontLabel/FontLabelStringDrawing.h +++ b/cocos2dx/platform/ios/FontLabel/FontLabelStringDrawing.h @@ -75,6 +75,7 @@ @interface FontLabelStringDrawingHelper : NSObject { } + (CGSize)sizeWithZFont:(NSString*)string zfont:(ZFont *)font; ++ (CGSize)sizeWithZFont:(NSString *)string zfont:(ZFont *)font constrainedToSize:(CGSize)size; + (CGSize)drawInRect:(NSString*)string rect:(CGRect)rect withZFont:(ZFont *)font lineBreakMode:(UILineBreakMode)lineBreakMode alignment:(UITextAlignment)alignment; diff --git a/cocos2dx/platform/ios/FontLabel/FontLabelStringDrawing.m b/cocos2dx/platform/ios/FontLabel/FontLabelStringDrawing.m index e288b2bf70..2dde9823e4 100644 --- a/cocos2dx/platform/ios/FontLabel/FontLabelStringDrawing.m +++ b/cocos2dx/platform/ios/FontLabel/FontLabelStringDrawing.m @@ -899,6 +899,13 @@ static CGSize drawTextInRect(CGRect rect, NSString *text, NSArray *attributes, U return CGSizeMake(ceilf(size.width), ceilf(size.height)); } ++ (CGSize)sizeWithZFont:(NSString *)string zfont:(ZFont *)font constrainedToSize:(CGSize)size { + CGSize s = drawOrSizeTextConstrainedToSize(NO, string, attributeRunForFont(font), size, 0, UILineBreakModeWordWrap, UITextAlignmentLeft, YES); + return CGSizeMake(ceilf(s.width), ceilf(s.height)); +} + + + + (CGSize)drawInRect:(NSString*)string rect:(CGRect)rect withZFont:(ZFont *)font lineBreakMode:(UILineBreakMode)lineBreakMode alignment:(UITextAlignment)alignment { return [string drawInRect:rect withZFont:font lineBreakMode:lineBreakMode alignment:alignment];