Merge pull request #740 from minggo/iss762_label_position

fixed #762: make label display on center on iOS
This commit is contained in:
minggo 2012-03-01 19:19:12 -08:00
commit 30e952b093
3 changed files with 17 additions and 24 deletions

View File

@ -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,29 +356,8 @@ 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;
}
return dim;
}

View File

@ -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;

View File

@ -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];