bool CALabel::updateImage() { if (m_bRunning == false) { return false; } CAImage *tex = new CAImage(); if (!tex) { return false; } caFontDefinition fontDef = setFontDefiniton(true); CAImage *at = new CAImage(); at->initWithString("9m", fontDef.m_fontName.c_str(), fontDef.m_fontSize, CCSizeZero, fontDef.m_alignment, fontDef.m_vertAlignment); float fontHeight = at->getContentSize().height+1; at->release(); unsigned int linenumber = (int)this->getBounds().size.height/fontHeight; if (m_nNumberOfLine <= linenumber && m_nNumberOfLine != 0) { tex->initWithString(m_nText.c_str(), fontDef.m_fontName.c_str(), fontDef.m_fontSize, CCSizeZero, fontDef.m_alignment, fontDef.m_vertAlignment); } else { if (this->getBounds().size.width == 0 && this->getBounds().size.height == 0) { tex->initWithString(m_nText.c_str(), fontDef.m_fontName.c_str(), fontDef.m_fontSize, CCSizeZero, fontDef.m_alignment, fontDef.m_vertAlignment); } else { tex->initWithString(m_nText.c_str(), fontDef.m_fontName.c_str(), fontDef.m_fontSize, this->getBounds().size, fontDef.m_alignment, fontDef.m_vertAlignment); } } CCRect rect = CCRectZero; rect.size = tex->getContentSize(); m_cLabelSize = tex->getContentSize(); m_pTextImage->setImage(tex); m_pTextImage->setImageRect(rect); m_pTextImage->setColor(ccc3(m_nTextcolor.r, m_nTextcolor.g, m_nTextcolor.b)); m_pTextImage->setOpacity(m_nTextcolor.a); m_pTextImage->CAView::setFrameOrigin(CCPoint(0, (this->getBounds().size.height - rect.size.height)/2)); tex->release(); return true; }
void CALabel::updateImage() { CAImage *tex = new CAImage(); float fontHeight = getFontHeight(m_nfontName.c_str(), m_nfontSize); unsigned int linenumber = (int)this->getBounds().size.height/fontHeight; CCSize size = CCSizeZero; if ((m_nNumberOfLine <= linenumber && m_nNumberOfLine != 0)) { size = CCSizeMake(this->getBounds().size.width, fontHeight*m_nNumberOfLine); } else { size = this->getBounds().size; } tex->initWithString(m_nText.c_str(), m_nfontName.c_str(), m_nfontSize* CC_CONTENT_SCALE_FACTOR(), size, m_nTextAlignment, m_nVerticalTextAlignmet); m_cLabelSize = tex->getContentSize(); CCRect rect = CCRectZero; rect.size.width = this->getBounds().size.width; rect.size.height = tex->getContentSize().height; float width = MIN(this->getBounds().size.width,tex->getContentSize().width); rect.size.width = width; this->setImage(tex); switch (m_nVerticalTextAlignmet) { case CAVerticalTextAlignmentTop: pTextHeight =0; break; case CAVerticalTextAlignmentCenter: pTextHeight =(this->getBounds().size.height-rect.size.height)/2; break; case CAVerticalTextAlignmentBottom: pTextHeight =this->getBounds().size.height - rect.size.height; break; default: break; } this->setImageRect(rect); tex->release(); return ; }
// Helper bool CCLabelTTF::updateTexture() { CAImage *tex; tex = new CAImage(); if (!tex) return false; #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID) || (CC_TARGET_PLATFORM == CC_PLATFORM_IOS) ccFontDefinition texDef = _prepareTextDefinition(true); tex->initWithString( m_string.c_str(), &texDef ); #else tex->initWithString( m_string.c_str(), m_pFontName->c_str(), m_fFontSize * CC_CONTENT_SCALE_FACTOR(), CC_SIZE_POINTS_TO_PIXELS(m_tDimensions), m_hAlignment, m_vAlignment); #endif // set the texture this->setImage(tex); // release it tex->release(); // set the size in the sprite CCRect rect =CCRectZero; rect.size = m_pobImage->getContentSize(); this->setImageRect(rect); //ok return true; }
NS_CC_BEGIN static unsigned int getFontHeight(const char *fontName, int fontSize) { unsigned int result=0; CAImage *image = new CAImage(); image->initWithString("9m", fontName, fontSize, CCSizeZero, CATextAlignmentLeft, CAVerticalTextAlignmentTop); CAImage *image1 = new CAImage(); image1->initWithString("9\nm", fontName, fontSize, CCSizeZero, CATextAlignmentLeft, CAVerticalTextAlignmentTop); result = image1->getContentSize().height-image->getContentSize().height; image->release(); image1->release(); return result; }