|
- --
- -- Author: songge
- -- Date: 2016-04-11 20:36:27
- --
- -- 用法:
- -- local text = "faff人 {1} f人 {2} KKK"
- -- self._tDetail = cc.Label:createWithTTF("进度", "res/fonts/huakangziti.TTF", 24)
- -- :align(display.CENTER, cc.p(display.width/2, 40))
- -- :addTo(self)
- -- :setColor(display.COLOR_WHITE)
- -- local table = {["{1}"] = "替1换",
- -- ["{2}"] = "a替2换吖"}
- -- LabelTool.changeLabelColor(self._tDetail, text, table)
-
- local LabelTool = {}
- --根据显示宽度截取字符串,添加省略号
- -- 拿字符串字符数量 中文2 英文1
- function LabelTool.GetUTF8StrNums( str )
- if (not str) then
- return
- end
- local strLen = #str
- local index, numTmp = 1, 0
- local t = 1
- for i=1,strLen do
- if index <= i then
- local curByte = string.byte(str, i)
- local byteCount = 1
- numTmp = numTmp + 2
- if curByte>0 and curByte<=127 then
- byteCount = 1
- numTmp = numTmp - 1
- elseif curByte>=192 and curByte<=223 then
- byteCount = 2
- elseif curByte>=224 and curByte<=239 then
- byteCount = 3
- elseif curByte>=240 and curByte<=247 then
- byteCount = 4
- end
- -- print("______",byteCount,curByte)
- index = index + byteCount
- end
- end
- return numTmp
- end
-
- --[[
- 获取一个字符串的从第from个字符开始的num个字符
- 中文2 英文1个字符
- ]]
- function LabelTool.GetUTF8StrByIdxNumEx( str, num )
- if (not str) or (not num) then
- return ""
- end
- if num <= 0 then
- return str
- end
- local strLen = #str
- local index, numTmp = 1, 0
- local t = 1
- for i=1,strLen do
- if numTmp >= num then
- break
- end
- if index <= i then
- local curByte = string.byte(str, i)
- local byteCount = 1
- numTmp = numTmp + 2
- if curByte>0 and curByte<=127 then
- byteCount = 1
- numTmp = numTmp - 1
- elseif curByte>=192 and curByte<=223 then
- byteCount = 2
- elseif curByte>=224 and curByte<=239 then
- byteCount = 3
- elseif curByte>=240 and curByte<=247 then
- byteCount = 4
- end
- index = index + byteCount
- end
- end
- return string.sub(str, 1, index-1)
- end
- --text
- function LabelTool.changeLabelColorWithTable(__label, __table, __color)
- local _text = __label:getString()
- for i=1, #__table do
- local posBegin = string.find(_text, __table[i])
- if (not posBegin) then
- return
- end
- local pos = string.utf8len(string.sub(_text, 0, posBegin-1))
- --print("找到了当前的 ", __table[i], posBegin)
- for m=1, string.utf8len(__table[i]) do
- --print("找到了当前的 111 ", __table[i], m)
- local sp = __label:getLetter(pos+m-1)
- if (sp) then
- sp:setColor(__color)
- end
- end
- end
- end
- function LabelTool.changeLabelColor(__label, __text, __table, changeColor)
- assert(__label~=nil,"labeltool __label is nil")
- assert(__text~=nil,"labeltool __text is nil")
- assert(__table~=nil and type(__table)=="table", "labeltool __table is error")
-
- local allBegins, allEnds = {},{}
- local changeText = __text
- local num = table.nums(__table)
- for i=1, num do
- local _strOld = "{"..i.."}"
- local _strNew = __table[_strOld]
- local posBegin = string.find(changeText, _strOld)
- if (posBegin==nil) then
- --print("error, posBegin == nil")
- __label:setString(__text)
- break
- end
- local pos = LabelTool.countCNWords(string.sub(changeText, 0, posBegin-1))
- -- print("替换后")
- -- print("pos = "..pos.." _strNew length = "..LabelTool.countCNWords(_strNew))
- if (pos>-1) then
- allBegins[#allBegins+1] = pos
- --print("allBegins = "..allBegins[#allBegins])
- local posEnd = pos + LabelTool.countCNWords(_strNew)-1
- allEnds[#allEnds+1] = posEnd
- --print("allEnds = "..allEnds[#allBegins])
-
- changeText = string.gsub(changeText, _strOld, _strNew)
- end
- end
- __label:setString(changeText)
- for i=1, #allBegins do
- for j=allBegins[i], allEnds[i] do
- local sp = __label:getLetter(j)
- --print("allBegins = "..j)
- if (sp) then
- changeColor = changeColor or G_BaseDefine.MAJIANG_COLOR_SEL
- sp:setColor(changeColor)
- else
- -- print("error sp")
- end
- end
- end
- end
-
- -- 计算长度,中文字符长度只占一个
- function LabelTool.countCNWords(__pszText)
- local length = string.utf8len(__pszText)
- return length
- end
-
- -- 输入框
- function LabelTool.getEditBox(__bgPath, __size, __str, __inputMode, __maxLen, __fontSize)
- __maxLen = __maxLen or 15
- __fontSize = __fontSize or 24
-
- local editPhone = cc.TextField:createNode()
- editPhone:setDefaults()
- local ttfConfig = {}
- ttfConfig.fontFilePath = "res/default/msyh.ttc"
- ttfConfig.fontSize = __fontSize
- ttfConfig.glyphs = cc.GLYPHCOLLECTION_DYNAMIC
- ttfConfig.customGlyphs = nil
- ttfConfig.distanceFieldEnabled = false
- ttfConfig.outlineSize = 0
- ttfConfig.texColor = display.COLOR_BLACK
-
- editPhone:setFontConfig(ttfConfig);
- editPhone:setText("")
- editPhone:setColor(display.COLOR_BLACK)
- -- editPhone:setAnchorPoint(cc.p(0, 0.5))
- editPhone:setSize(__size)
- if __str then
- editPhone:setPlaceHolder(__str)
- end
- editPhone:setMaxLengthEnabled(true)
- editPhone:setMaxLength(__maxLen)
- return editPhone
- end
-
- -- 针对公告改变颜色
- -- <> cc.RED = cc.c3b(255,0,0)
- -- () cc.GREEN = cc.c3b(0,255,0)
- -- [] cc.BLUE = cc.c3b(0,0,255)
-
- function LabelTool.changeLabelColorEx(__label, __text)
- -- __text = "(ji(an)g03;V$#asahdj是手机号)犹如赌神附体,在[上海敲麻]中赢了{18}倍!"
- local changeTbl = {}
- local name = ""
- local namebpos
-
- -- 玩家名中可能存在[]和{} 单独拿出来处理
- local bp, ep, cstr = string.find(__text, "(%(.+%)+)")
- -- print("kkk----", bp, ep, cstr)
- if cstr then
- name = string.sub(cstr, 2, -2)
- local s1 = string.sub(__text, 0, bp-1)
- local s2 = string.sub(__text, ep+1, #__text)
- __text = s1 .. "%s" .. s2
- -- print(s1, s2, "==")
-
- namebpos = math.max(bp, 0)
-
- local posh,pose = string.find(name, ";V$#")
- if posh and pose then
- name = string.sub(name, 1, posh-1)
- end
- end
-
- for catchStr in string.gmatch(__text, '(%b[])') do
- local str = string.gsub(catchStr, "[%[%]]","")
- changeTbl[str] = GONGGAO_COLOR_GAME
- end
- for catchStr in string.gmatch(__text, '(%b{})') do
- local str = string.gsub(catchStr, "[{}]","")
- changeTbl[str] = GONGGAO_COLOR_NUM
- end
-
- local _text = string.gsub(__text, "[{}%[%]]", "")
- _text = string.format(_text, name)
- __label:setString(_text)
- -- print("changeLabelColorEx", _text)
- -- dump(changeTbl)
-
- local function changeColor(bpos, epos, color)
- if bpos and epos then
- if bpos ~= 0 then
- bpos = LabelTool.countCNWords(string.sub(_text, 0, bpos-1))
- end
- epos = LabelTool.countCNWords(string.sub(_text, 0, epos)) - 1
- for index = bpos, epos do
- local sp = __label:getLetter(index)
- if (sp) then
- sp:setColor(color)
- else
- -- print("error sp")
- end
- end
- end
- end
-
- for k, v in pairs(changeTbl) do
- local bpos, epos = string.find(_text, k)
- -- print(bpos, epos, k)
- changeColor(bpos, epos, v)
- end
- if namebpos then
- -- print(namebpos, #name, string.len(name),"000")
- changeColor(namebpos, namebpos+(string.len(name)-1), GONGGAO_COLOR_NAME)
- end
- end
-
- return LabelTool
|