issue #347: FontTest runs ok on iOS

This commit is contained in:
minggo 2011-08-01 18:22:47 +08:00
parent a3b4043755
commit 895d4992e4
4 changed files with 49 additions and 18 deletions

View File

@ -21,15 +21,17 @@ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
#include <Foundation/Foundation.h>
#include <UIKit/UIKit.h>
#include "CCImage.h"
#include "CCFileUtils.h"
#include <string>
#import "CCImage.h"
#import "CCFileUtils.h"
#import <string>
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#if CC_FONT_LABEL_SUPPORT
// FontLabel support
#include "FontManager.h"
#include "FontLabelStringDrawing.h"
#import "FontLabel/FontManager.h"
#import "FontLabel/FontLabelStringDrawing.h"
#endif// CC_FONT_LABEL_SUPPORT
typedef struct
@ -340,15 +342,16 @@ static bool _initWithString(const char * pText, cocos2d::CCImage::ETextAlign eAl
{
CC_BREAK_IF(! pText || ! pInfo);
NSString * string = [NSString stringWithUTF8String:pText];
NSString * str = [NSString stringWithUTF8String:pText];
NSString * fntName = [NSString stringWithUTF8String:pFontName];
CGSize dim;
// create the font
id font = [UIFont fontWithName:fntName size:nSize];
// create the font
id font;
font = [UIFont fontWithName:fntName size:nSize];
if (font)
{
dim = [string sizeWithFont:font];
dim = [str sizeWithFont:font];
}
#if CC_FONT_LABEL_SUPPORT
@ -357,7 +360,8 @@ static bool _initWithString(const char * pText, cocos2d::CCImage::ETextAlign eAl
font = [[FontManager sharedManager] zFontWithName:fntName pointSize:nSize];
if (font)
{
dim = [string sizeWithZFont:font];
//dim = [str sizeWithZFont:font];
dim = [FontLabelStringDrawingHelper sizeWithZFont:str zfont:font];
}
}
#endif // CC_FONT_LABEL_SUPPORT
@ -374,7 +378,7 @@ static bool _initWithString(const char * pText, cocos2d::CCImage::ETextAlign eAl
if (font)
{
dim = [string sizeWithFont:font];
dim = [str sizeWithFont:font];
}
}
@ -412,7 +416,6 @@ static bool _initWithString(const char * pText, cocos2d::CCImage::ETextAlign eAl
// measure text size with specified font and determine the rectangle to draw text in
unsigned uHoriFlag = eAlign & 0x0f;
unsigned uVertFlag = (eAlign & 0xf0) >> 4;
UITextAlignment align = (2 == uHoriFlag) ? UITextAlignmentRight
: (3 == uHoriFlag) ? UITextAlignmentCenter
: UITextAlignmentLeft;
@ -420,13 +423,14 @@ static bool _initWithString(const char * pText, cocos2d::CCImage::ETextAlign eAl
// normal fonts
if( [font isKindOfClass:[UIFont class] ] )
{
[string drawInRect:CGRectMake(0, 0, dim.width, dim.height) withFont:font lineBreakMode:UILineBreakModeWordWrap alignment:align];
[str drawInRect:CGRectMake(0, 0, dim.width, dim.height) withFont:font lineBreakMode:UILineBreakModeWordWrap alignment:align];
}
#if CC_FONT_LABEL_SUPPORT
else // ZFont class
{
[string drawInRect:CGRectMake(0, 0, dim.width, dim.height) withZFont:font lineBreakMode:UILineBreakModeWordWrap alignment:align];
//[str drawInRect:CGRectMake(0, 0, dim.width, dim.height) withZFont:font lineBreakMode:UILineBreakModeWordWrap alignment:align];
[FontLabelStringDrawingHelper drawInRect:str rect:CGRectMake(0, 0, dim.width, dim.height) withZFont:font lineBreakMode:UILineBreakModeWordWrap alignment:align];
}
#endif

View File

@ -4,6 +4,7 @@
//
// Created by Kevin Ballard on 5/5/09.
// Copyright © 2009 Zynga Game Networks
// Copyright (c) 2011 cocos2d-x.org
//
//
// Licensed under the Apache License, Version 2.0 (the "License");
@ -67,3 +68,14 @@
- (CGSize)drawInRect:(CGRect)rect withLineBreakMode:(UILineBreakMode)lineBreakMode alignment:(UITextAlignment)alignment
numberOfLines:(NSUInteger)numberOfLines;
@end
// This class is used to invoke in .mm file.
// Can not invoke FontLabelStringDrawing directly in .mm.
// It seems that, in .mm it can not support category.
@interface FontLabelStringDrawingHelper : NSObject {
}
+ (CGSize)sizeWithZFont:(NSString*)string zfont:(ZFont *)font;
+ (CGSize)drawInRect:(NSString*)string rect:(CGRect)rect withZFont:(ZFont *)font
lineBreakMode:(UILineBreakMode)lineBreakMode
alignment:(UITextAlignment)alignment;
@end

View File

@ -4,6 +4,7 @@
//
// Created by Kevin Ballard on 5/5/09.
// Copyright © 2009 Zynga Game Networks
// Copyright (c) 2011 cocos2d-x.org
//
//
// Licensed under the Apache License, Version 2.0 (the "License");
@ -890,3 +891,17 @@ static CGSize drawTextInRect(CGRect rect, NSString *text, NSArray *attributes, U
return drawTextInRect(rect, self.string, self.attributes, lineBreakMode, alignment, numberOfLines, NO);
}
@end
@implementation FontLabelStringDrawingHelper
+ (CGSize)sizeWithZFont:(NSString*)string zfont:(ZFont *)font {
CGSize size = drawOrSizeTextConstrainedToSize(NO, string, attributeRunForFont(font), CGSizeMake(CGFLOAT_MAX, CGFLOAT_MAX), 1,
UILineBreakModeClip, UITextAlignmentLeft, YES);
return CGSizeMake(ceilf(size.width), ceilf(size.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];
}
@end

View File

@ -460,7 +460,7 @@
NSRange subrange = NSMakeRange(first, [_attributes count] - first);
if (NSMaxRange(range) < [_buffer length]) {
ZAttributeRun *newRun = [[ZAttributeRun alloc] initWithIndex:NSMaxRange(range)
attributes:(NSDictionary*)[[_attributes lastObject] attributes]];
attributes:(NSDictionary*)[(ZAttributeRun *)[_attributes lastObject] attributes]];
[_attributes addObject:newRun];
[newRun release];
}
@ -484,7 +484,7 @@
if ([[_attributes objectAtIndex:firstAfter] index] > NSMaxRange(range)) {
// the first after is too far after, insert another run!
ZAttributeRun *newRun = [[ZAttributeRun alloc] initWithIndex:NSMaxRange(range)
attributes:(NSDictionary*)[[_attributes objectAtIndex:firstAfter-1] attributes]];
attributes:[(ZAttributeRun *)[_attributes objectAtIndex:firstAfter-1] attributes]];
[_attributes insertObject:newRun atIndex:firstAfter];
[newRun release];
}