您的位置:首页 > 移动开发 > Cocos引擎

Cocos2dx lua Label的换行

2015-01-07 11:00 591 查看
参考了star特530的博客。原文链接

按照上文去做,如果遇到文本是中英混合就会出现中文乱码。以下是改进后的方法

-- Label文本换行
function FunSetLinefeed( strText, nLineWidth )		--文本,行宽
--读取每个字符做中文英文判断,并且记录大小
local nStep = 1
local index = 1
local ltabTextSize = {}
while true do
c = string.sub(strText, nStep, nStep)
b = string.byte(c)

if b > 128 then
ltabTextSize[index] = 3
nStep = nStep + 3
index = index + 1
else
ltabTextSize[index] = 1
nStep = nStep + 1
index = index + 1
end

if nStep > #strText then
break
end
end

--将字符按照限定行宽进行分组
local nLineCount = 1
local nBeginPos = 1
local lptrCurText = nil
local ltabText = {}
local nCurSize = 0
for i = 1, index - 1 do
nCurSize = nCurSize + ltabTextSize[i]
if nCurSize > nLineWidth then
nCurSize = nCurSize - ltabTextSize[i]
ltabText[nLineCount] = string.sub( strText, nBeginPos, nBeginPos + nCurSize - 1 )
nBeginPos = nBeginPos + nCurSize
nCurSize = ltabTextSize[i]
nLineCount = nLineCount + 1
end
end
for i = 1, nLineCount - 1 do
if lptrCurText == nil then
lptrCurText = ltabText[i]
else
lptrCurText = lptrCurText .. "\n" .. ltabText[i]
end
end
return lptrCurText
end
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  cocos2dx lua label