兄臺,characterCountVisible*3 不太合理,不支持不同字節長度混合字符, 這里最好計算 characterCountVisible 個字符的真實字節長度,再用string.sub截取,比較合理。
-- 獲取指定字符個數的字節長度 return 當前count位字符串實際字節長度, 最后一個字符的字節長度
function string.sbytelen(input, count)
local len = string.len(input);
local left = len;
local actual = 0;
local cur = 0;
local cnt = 0;
local arr = {0, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc};
count = count or len;
while left ~= 0 do
local tmp = string.byte(input, -left);
local i = #arr;
cur = 0;
while arr[i] do
if tmp >= arr[i] then
cur = i;
left = left - i;
break;
end
i = i - 1;
end
cnt = cnt + 1;
actual = actual + cur;
if cnt >= count then
break;
end
end
return actual, cur;
end
unity Text文本超框在末尾顯示“...”