mirror of https://github.com/axmolengine/axmol.git
[iphone] test alignment in labels, ref to the post http://www.cocos2d-x.org/boards/6/topics/588?r=589#message-589 Thanks to Slaz
This commit is contained in:
parent
626a3f8b74
commit
693a8f049b
|
@ -35,7 +35,7 @@ namespace cocos2d{
|
||||||
CCXBitmapDC();
|
CCXBitmapDC();
|
||||||
CCXBitmapDC(const char *text,
|
CCXBitmapDC(const char *text,
|
||||||
CGSize dimensions = CGSizeZero,
|
CGSize dimensions = CGSizeZero,
|
||||||
UITextAlignment alignment = UITextAlignmentCenter,
|
cocos2d::UITextAlignment alignment = UITextAlignmentCenter,
|
||||||
const char *fontName = NULL,
|
const char *fontName = NULL,
|
||||||
float fontSize = 0);
|
float fontSize = 0);
|
||||||
~CCXBitmapDC(void);
|
~CCXBitmapDC(void);
|
||||||
|
|
|
@ -57,31 +57,39 @@ out:
|
||||||
}
|
}
|
||||||
|
|
||||||
static void initWithString(const char *content, const char *fontName, float size, /*input params*/
|
static void initWithString(const char *content, const char *fontName, float size, /*input params*/
|
||||||
int *pWidth, int *pHeight, unsigned char** ppData) /*output params*/
|
int *pWidth, int *pHeight, UITextAlignment alignment, unsigned char** ppData) /*output params*/
|
||||||
{
|
{
|
||||||
NSUInteger width, height;
|
NSUInteger width, height;
|
||||||
unsigned char* data;
|
unsigned char* data;
|
||||||
CGContextRef context;
|
CGContextRef context;
|
||||||
CGColorSpaceRef colorSpace;
|
CGColorSpaceRef colorSpace;
|
||||||
id uiFont;
|
id uiFont;
|
||||||
NSString *string;
|
NSString *string;
|
||||||
CGSize dimensions;
|
CGSize dimensions;
|
||||||
UITextAlignment alignment;
|
NSString *name;
|
||||||
NSString *name;
|
|
||||||
|
|
||||||
if (!pWidth || !pHeight || !ppData)
|
if (!pWidth || !pHeight || !ppData)
|
||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
alignment = UITextAlignmentCenter;
|
|
||||||
string = [[NSString alloc] initWithUTF8String:content];
|
string = [[NSString alloc] initWithUTF8String:content];
|
||||||
//name = isValidFontName(fontName) ? [[NSString alloc] initWithUTF8String:fontName] : @"Thonburi";
|
|
||||||
name = isValidFontName(fontName) ? [[NSString alloc] initWithUTF8String:fontName] : @"MarkerFelt-Wide";
|
name = isValidFontName(fontName) ? [[NSString alloc] initWithUTF8String:fontName] : @"MarkerFelt-Wide";
|
||||||
dimensions = [string sizeWithFont:[UIFont fontWithName:name size:size]];
|
dimensions = [string sizeWithFont:[UIFont fontWithName:name size:size]];
|
||||||
|
|
||||||
width = dimensions.width;
|
width = *pWidth;
|
||||||
height = dimensions.height;
|
height = *pHeight;
|
||||||
|
|
||||||
|
// use whatever is larger, the dimension calculated, or the one passed in
|
||||||
|
if ( dimensions.width > *pWidth )
|
||||||
|
{
|
||||||
|
width = dimensions.width;
|
||||||
|
}
|
||||||
|
if ( dimensions.height > *pHeight )
|
||||||
|
{
|
||||||
|
height = dimensions.height;
|
||||||
|
}
|
||||||
|
|
||||||
colorSpace = CGColorSpaceCreateDeviceRGB();
|
colorSpace = CGColorSpaceCreateDeviceRGB();
|
||||||
data = new unsigned char[height * width * 4];
|
data = new unsigned char[height * width * 4];
|
||||||
|
@ -99,8 +107,8 @@ static void initWithString(const char *content, const char *fontName, float size
|
||||||
CGRect rect;
|
CGRect rect;
|
||||||
rect.origin.x = 0;
|
rect.origin.x = 0;
|
||||||
rect.origin.y = 0;
|
rect.origin.y = 0;
|
||||||
rect.size.width = dimensions.width;
|
rect.size.width = width;
|
||||||
rect.size.height = dimensions.height;
|
rect.size.height = height;
|
||||||
[string drawInRect:rect withFont:uiFont lineBreakMode:UILineBreakModeWordWrap alignment:alignment];
|
[string drawInRect:rect withFont:uiFont lineBreakMode:UILineBreakModeWordWrap alignment:alignment];
|
||||||
|
|
||||||
UIGraphicsPopContext();
|
UIGraphicsPopContext();
|
||||||
|
@ -127,7 +135,9 @@ namespace cocos2d {
|
||||||
|
|
||||||
CCXBitmapDC::CCXBitmapDC(const char *text, CGSize dimensions, UITextAlignment alignment, const char *fontName, float fontSize)
|
CCXBitmapDC::CCXBitmapDC(const char *text, CGSize dimensions, UITextAlignment alignment, const char *fontName, float fontSize)
|
||||||
{
|
{
|
||||||
initWithString(text, fontName, fontSize, &m_nWidth, &m_nHeight, &m_pData);
|
m_nWidth = dimensions.width;
|
||||||
|
m_nHeight = dimensions.height;
|
||||||
|
initWithString(text, fontName, fontSize, &m_nWidth, &m_nHeight, (::UITextAlignment)alignment, &m_pData);
|
||||||
}
|
}
|
||||||
|
|
||||||
CCXBitmapDC::~CCXBitmapDC()
|
CCXBitmapDC::~CCXBitmapDC()
|
||||||
|
|
Loading…
Reference in New Issue