工作中遇到的问题,记录一下
cocos2d的Text控件通过一些设置后可以实现自动换行,具体代码如下:
local toastTxt = ccui.Text:create()
toastTxt:ignoreContentAdaptWithSize(false)
toastTxt:setTextAreaSize(cc.size(200,200))
如果不使用上面的设置,自己用代码来实现,思路很简单,就是截取一段长度的字符串后中末尾加一个换行符,一直重复,直到取完整个串,下面提供一下我的思路
-- 分解成多行(文本,一行最大长度)
function utils:splitLine(str,chatSize,maxWidthNum)
maxWidthNum = maxWidthNum or 560
chatSize = chatSize or 24
--str = string.gsub(str, "%s+", "")
local strLen = string.len(str)
local newText = ""
local tempStr = ""
local len = 0
local text = ccui.Text:create("", "font/arial.ttf", chatSize)
for i=1,strLen do
tempStr = tempStr..string.sub(str,i,i)
print(tempStr)
text:setString(tempStr)
local shardWidth = text:getContentSize().width
if shardWidth >= maxWidthNum then
newText = newText..tempStr.."\n"
tempStr = ""
len = len + 1
end
if i == strLen then
newText = newText..tempStr
end
end
return newText
end
使用上面的方法确实可以解决大部分问题,还有个问题,在行尾比如 “ignoreContent” 可能就会拆成 “ignore”“Content”,大概是这个意思。解决思路也简单,就是把要截取的串里的单词全部拆开,然后再重新组装:
--分解成多行,句尾保留单词完整(文本,一行最大长度)
function utils:splitLineCode(str,maxWidthNum)
maxWidthNum = maxWidthNum or 560
local tStr = {}
local len = string.len(str)
local curIdx = 1
local be,ed = 1,1
while curIdx<=len do
be,ed = string.find(str," ")
if not be and not ed then
table.insert(tStr, str)
break
else
local s = string.sub(str, 1, ed-1)
if s ~= "" then
table.insert(tStr, s.." ")--补个空格
end
curIdx = curIdx + ed
str = string.sub(str, ed+1, len)
end
end
local text = ccui.Text:create("", 32)
local newStr = ""
local curLen = 0
local line = 0
for k,v in pairs(tStr) do
text:setString(v)
local sl = text:getContentSize().width
if curLen + sl <= maxWidthNum then
curLen = curLen + sl
newStr = newStr..v
else
newStr = newStr.."\n"..v
curLen = 0
line = line + 1
end
end
return newStr,line
end
或者在被截断的单词后main加个“-”,
--分解成多行,句尾保留单词完整(文本,一行最大长度)
function utils:splitLineCode1(str,maxWidthNum)
maxWidthNum = maxWidthNum or 40
local tStr = {}
local len = string.len(str)
local curIdx = 1
local be,ed = 1,1
while curIdx<=len do
be,ed = string.find(str," ")
if not be and not ed then
table.insert(tStr, str)
break
else
local s = string.sub(str, 1, ed-1)
if s ~= "" then
table.insert(tStr, s)--补个空格
end
curIdx = curIdx + ed
str = string.sub(str, ed+1, len)
end
end
local strTab = {}
local newStr = ""
local curLen = 0
local line = 1
for k,v in pairs(tStr) do
local sl = string.len(v)
curLen = string.len(newStr)
if curLen + sl < maxWidthNum then
newStr = newStr..v.." "
if k == #tStr then
table.insert(strTab,newStr)
end
else
local needLen = maxWidthNum - curLen
if needLen == 0 then
table.insert(strTab,newStr.."\n")
newStr = v
else
local addStr = string.sub(v,1,needLen-1)
local subStr = string.sub(v,needLen,sl).." "
--判断末尾特殊字符“”,'
if subStr == " " or subStr == "\" " or subStr == "'" then
newStr = newStr..v
else
if string.len(newStr) == maxWidthNum-1 then
newStr = newStr..addStr
else
newStr = newStr..addStr.."-"
end
table.insert(strTab,newStr.."\n")
newStr = subStr
if k == #tStr then
table.insert(strTab,newStr)
end
end
end
end
end
local newStr = ""
for k,v in pairs(strTab) do
--print("长度:",string.len(v))
newStr = newStr..v
end
return newStr,#strTab
end
就记录这么多了,希望能给你提供一下思路,有不对的地方请指正。