有一段如:“是一句测试,这pppppp[[[[[[aaaaxxxxxx,这是一句测试,这是一句测试,这是一句测试,这是一句测试,这是一句测试.”这样的中英文混合在一起的字符串,需要显示在游戏中:
local MainScene = class("MainScene", cc.load("mvc").ViewBase)
function MainScene:ctor()
local str = '是一句测试,这pppppp[[[[[[aaaaxxxxxx,这是一句测试,这是一句测试,这是一句测试,这是一句测试,这是一句测试.'
local label = self.newTTFLabel({
text = str,font = "fonts/DFYUANW7-GB2312.ttf",size = 20})
label:setAnchorPoint(0,0)
self:addChild(label)
label:setPosition(200,200)
end
function MainScene.newTTFLabel(config)
local label = nil
if config then
if config.font then
local mytext = ""
local color = cc.c3b(255, 255, 255)
local size = 22
local align = cc.TEXT_ALIGNMENT_CENTER
local vertical = cc.VERTICAL_TEXT_ALIGNMENT_CENTER
if config.text then
mytext = config.text
end
if config.color then
color = config.color
end
if config.size then
size = config.size
end
if config.vertical then
vertical = config.vertical
end
label = cc.Label:createWithTTF(mytext,config.font,size)
end
end
return label
end
return MainScene
如果显示区域有限制,需要只显示其中的一段并在后面补“……”,直接使用string.sub(str,1,#str/2):
local subStr = string.sub(str,1,#str/2)
print('--string.sub(str,1,#str/2)--',subStr)
local label = self.newTTFLabel({text = subStr,font = "fonts/DFYUANW7-GB2312.ttf",size = 20})
label:setAnchorPoint(0,0)
self:addChild(label)
label:setPosition(200,200)
结果
[LUA-print] --string.