以固定寬度填充ccui.Text,寬度不夠時會截取並添加ending(...)

-- 以固定寬度填充ccui.Text,寬度不夠時會截取並添加ending
-- width可選,默認獲取field的寬度
-- ending可選,默認使用"..."
function ViewUtil.fixedWidthText(field, str, width, ending)--控件、字符串、控件顯示文本的寬度、以什麼結尾
    if type(width) ~= "number" then
        width = field:getTextAreaSize().width
        if width == 0 then
            width = 999999 -- 能多長就多長
        end
    end
    if type(ending) ~= "string" then
        ending = "..."
    end

    local chars = UTF8.utf8chartable(tostring(str))
    local endChars = UTF8.utf8chartable(ending)
    local fontSize = field:getFontSize()
    
    local endLen = 0
    for i, v in ipairs(endChars) do
        endLen = endLen + (UTF8.utf8charbytes(v) > 1 and 1 or 0.5)
    end
    local len = 1
    for i = 1, #chars - 1 do
        len = len + (UTF8.utf8charbytes(chars[i]) > 1 and 1 or 0.5)
        if (len + endLen) * fontSize > width then
            local ic = ccui.Text:create("", field:getFontName(), fontSize)
            while i >= 1 do
                ic:setString(table.concat(chars, "", 1, i)..ending)
                if ic:getVirtualRendererSize().width > width then
                    i = i - 1
                else
                    field:setString(table.concat(chars, "", 1, i)..ending)
                    return
                end
            end
            field:setString(chars[1]..ending)
            return
        end
    end
    field:setString(tostring(str))
end
local UTF8 = {}

function UTF8.utf8charbytes (s, i)
    -- argument defaults
    i = i or 1
    local c = string.byte(s, i)
 
    -- determine bytes needed for character, based on RFC 3629
    if c > 0 and c <= 127 then
        -- UTF8-1
        return 1
    elseif c >= 194 and c <= 223 then
        -- UTF8-2
        local c2 = string.byte(s, i + 1)
        return 2
    elseif c >= 224 and c <= 239 then
        -- UTF8-3
        local c2 = s:byte(i + 1)
        local c3 = s:byte(i + 2)
        return 3
    elseif c >= 240 and c <= 244 then
        -- UTF8-4
        local c2 = s:byte(i + 1)
        local c3 = s:byte(i + 2)
        local c4 = s:byte(i + 3)
        return 4
    end
end
 
-- returns the number of characters in a UTF-8 string
function UTF8.utf8len (s)
    local pos = 1
    local bytes = string.len(s)
    local len = 0
 
    while pos <= bytes and len ~= chars do
        local c = string.byte(s,pos)
        len = len + 1
 
        pos = pos + UTF8.utf8charbytes(s, pos)
    end
 
    if chars ~= nil then
        return pos - 1
    end
 
    return len
end
 
-- functions identically to string.sub except that i and j are UTF-8 characters
-- instead of bytes
function UTF8.utf8sub (s, i, j)
    j = j or -1
 
    if i == nil then
        return ""
    end
 
    local pos = 1
    local bytes = string.len(s)
    local len = 0
 
    -- only set l if i or j is negative
    local l = (i >= 0 and j >= 0) or UTF8.utf8len(s)
    local startChar = (i >= 0) and i or l + i + 1
    local endChar = (j >= 0) and j or l + j + 1
 
    -- can't have start before end!
    if startChar > endChar then
        return ""
    end
 
    -- byte offsets to pass to string.sub
    local startByte, endByte = 1, bytes
 
    while pos <= bytes do
        len = len + 1
 
        if len == startChar then
            startByte = pos
        end
 
        pos = pos + UTF8.utf8charbytes(s, pos)
 
        if len == endChar then
            endByte = pos - 1
            break
        end
    end
 
    return string.sub(s, startByte, endByte)
end
 
-- replace UTF-8 characters based on a mapping table
function UTF8.utf8replace (s, mapping)
    local pos = 1
    local bytes = string.len(s)
    local charbytes
    local newstr = ""
 
    while pos <= bytes do
        charbytes = UTF8.utf8charbytes(s, pos)
        local c = string.sub(s, pos, pos + charbytes - 1)
        newstr = newstr .. (mapping[c] or c)
        pos = pos + charbytes
    end
 
    return newstr
end

-- get a table of characters in UTF-8.
function UTF8.utf8chartable(s)
    local ret = {}
    local pos = 1
    local bytes = string.len(s)

    while pos <= bytes do
        local len = UTF8.utf8charbytes(s, pos)
        table.insert(ret, string.sub(s, pos, pos + len - 1))
        pos = pos + len
    end

    return ret
end

return UTF8


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章