cocos2dx文本支持不同颜色的方法

ColorLabel = {}
ColorLabel.__index = ColorLabel

function ColorLabel:create( w , h , fontSize )

instance = {}

setmetatable( instance , ColorLabel )
instance.mSize = CCSize( w , h )
instance.mPoint = ccp(0,h)
instance.rt = CCRenderTexture:create( w , h )
instance.fontSize = fontSize
instance.layer = CCSprite:create()
instance.layer:setContentSize( CCSize( w , h ) )
instance.layer:addChild( instance.rt )
return instance

end

function ColorLabel:addString( str , fontName , fontColor )

local pos = 1
local fontSize = self.fontSize
local width = self.fontSize
local point = self.mPoint
while pos <= str:len() do
    local l = 2
    if string.byte(str, pos) >0x80 then
        l=3
        width = fontSize
    else
        l=1
        width = fontSize / 2
    end
    local label = CCLabelTTF:create(str:sub(pos , pos+l-1),fontName,fontSize)
    label:setColor(fontColor)
    label:setAnchorPoint( ccp(0,1) )
    self.rt:begin()
    label:setPosition( point )
    label:visit()
    self.rt:endToLua()
    pos = pos + l
    point.x = point.x + width
    if point.x > self.mSize.width - width then
        point.x = 0
        point.y = point.y - fontSize
    end

end
self.mPoint = point

end

–[[

cl = ColorLabel:create( 200 , 200 , 40 )

cl.layer:setPosition(ccp(500,500))

cl:addString( "【我是屌丝】" , "Black" , ccc3(255,255,255) )
cl:addString( "获得【武器装备啊】" , "Black" , ccc3(0,255,255) )


scene:addChild( cl.layer )

–]]

你可能感兴趣的:(cocosdx,文本不同颜色)