开发环境:cocos2dx 3.17
开发语言:lua
我们在开发过程中,经常会遇到字符串过长,显示的时候超出范围,我们可以用...来代替字符串后面的内容。

代码:

function getStrByLen(str, len)
    if str == nil or len == nil then
        return
    end
    local limitStr = str 
    if(string.len(limitStr) > len) then
        local sName = SubUTF8String(limitStr , len)
        return sName.."..."
    end
    return limitStr
end