coco2d-x文本自动换行

工作中遇到的问题,记录一下

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

就记录这么多了,希望能给你提供一下思路,有不对的地方请指正。

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值