You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

255 lines
8.0 KiB

  1. --
  2. -- Author: songge
  3. -- Date: 2016-04-11 20:36:27
  4. --
  5. -- 用法:
  6. -- local text = "faff人 {1} f人 {2} KKK"
  7. -- self._tDetail = cc.Label:createWithTTF("进度", "res/fonts/huakangziti.TTF", 24)
  8. -- :align(display.CENTER, cc.p(display.width/2, 40))
  9. -- :addTo(self)
  10. -- :setColor(display.COLOR_WHITE)
  11. -- local table = {["{1}"] = "替1换",
  12. -- ["{2}"] = "a替2换吖"}
  13. -- LabelTool.changeLabelColor(self._tDetail, text, table)
  14. local LabelTool = {}
  15. --根据显示宽度截取字符串,添加省略号
  16. -- 拿字符串字符数量 中文2 英文1
  17. function LabelTool.GetUTF8StrNums( str )
  18. if (not str) then
  19. return
  20. end
  21. local strLen = #str
  22. local index, numTmp = 1, 0
  23. local t = 1
  24. for i=1,strLen do
  25. if index <= i then
  26. local curByte = string.byte(str, i)
  27. local byteCount = 1
  28. numTmp = numTmp + 2
  29. if curByte>0 and curByte<=127 then
  30. byteCount = 1
  31. numTmp = numTmp - 1
  32. elseif curByte>=192 and curByte<=223 then
  33. byteCount = 2
  34. elseif curByte>=224 and curByte<=239 then
  35. byteCount = 3
  36. elseif curByte>=240 and curByte<=247 then
  37. byteCount = 4
  38. end
  39. -- print("______",byteCount,curByte)
  40. index = index + byteCount
  41. end
  42. end
  43. return numTmp
  44. end
  45. --[[
  46. 获取一个字符串的从第from个字符开始的num个字符
  47. 中文2 英文1个字符
  48. ]]
  49. function LabelTool.GetUTF8StrByIdxNumEx( str, num )
  50. if (not str) or (not num) then
  51. return ""
  52. end
  53. if num <= 0 then
  54. return str
  55. end
  56. local strLen = #str
  57. local index, numTmp = 1, 0
  58. local t = 1
  59. for i=1,strLen do
  60. if numTmp >= num then
  61. break
  62. end
  63. if index <= i then
  64. local curByte = string.byte(str, i)
  65. local byteCount = 1
  66. numTmp = numTmp + 2
  67. if curByte>0 and curByte<=127 then
  68. byteCount = 1
  69. numTmp = numTmp - 1
  70. elseif curByte>=192 and curByte<=223 then
  71. byteCount = 2
  72. elseif curByte>=224 and curByte<=239 then
  73. byteCount = 3
  74. elseif curByte>=240 and curByte<=247 then
  75. byteCount = 4
  76. end
  77. index = index + byteCount
  78. end
  79. end
  80. return string.sub(str, 1, index-1)
  81. end
  82. --text
  83. function LabelTool.changeLabelColorWithTable(__label, __table, __color)
  84. local _text = __label:getString()
  85. for i=1, #__table do
  86. local posBegin = string.find(_text, __table[i])
  87. if (not posBegin) then
  88. return
  89. end
  90. local pos = string.utf8len(string.sub(_text, 0, posBegin-1))
  91. --print("找到了当前的 ", __table[i], posBegin)
  92. for m=1, string.utf8len(__table[i]) do
  93. --print("找到了当前的 111 ", __table[i], m)
  94. local sp = __label:getLetter(pos+m-1)
  95. if (sp) then
  96. sp:setColor(__color)
  97. end
  98. end
  99. end
  100. end
  101. function LabelTool.changeLabelColor(__label, __text, __table, changeColor)
  102. assert(__label~=nil,"labeltool __label is nil")
  103. assert(__text~=nil,"labeltool __text is nil")
  104. assert(__table~=nil and type(__table)=="table", "labeltool __table is error")
  105. local allBegins, allEnds = {},{}
  106. local changeText = __text
  107. local num = table.nums(__table)
  108. for i=1, num do
  109. local _strOld = "{"..i.."}"
  110. local _strNew = __table[_strOld]
  111. local posBegin = string.find(changeText, _strOld)
  112. if (posBegin==nil) then
  113. --print("error, posBegin == nil")
  114. __label:setString(__text)
  115. break
  116. end
  117. local pos = LabelTool.countCNWords(string.sub(changeText, 0, posBegin-1))
  118. -- print("替换后")
  119. -- print("pos = "..pos.." _strNew length = "..LabelTool.countCNWords(_strNew))
  120. if (pos>-1) then
  121. allBegins[#allBegins+1] = pos
  122. --print("allBegins = "..allBegins[#allBegins])
  123. local posEnd = pos + LabelTool.countCNWords(_strNew)-1
  124. allEnds[#allEnds+1] = posEnd
  125. --print("allEnds = "..allEnds[#allBegins])
  126. changeText = string.gsub(changeText, _strOld, _strNew)
  127. end
  128. end
  129. __label:setString(changeText)
  130. for i=1, #allBegins do
  131. for j=allBegins[i], allEnds[i] do
  132. local sp = __label:getLetter(j)
  133. --print("allBegins = "..j)
  134. if (sp) then
  135. changeColor = changeColor or G_BaseDefine.MAJIANG_COLOR_SEL
  136. sp:setColor(changeColor)
  137. else
  138. -- print("error sp")
  139. end
  140. end
  141. end
  142. end
  143. -- 计算长度,中文字符长度只占一个
  144. function LabelTool.countCNWords(__pszText)
  145. local length = string.utf8len(__pszText)
  146. return length
  147. end
  148. -- 输入框
  149. function LabelTool.getEditBox(__bgPath, __size, __str, __inputMode, __maxLen, __fontSize)
  150. __maxLen = __maxLen or 15
  151. __fontSize = __fontSize or 24
  152. local editPhone = cc.TextField:createNode()
  153. editPhone:setDefaults()
  154. local ttfConfig = {}
  155. ttfConfig.fontFilePath = "res/default/msyh.ttc"
  156. ttfConfig.fontSize = __fontSize
  157. ttfConfig.glyphs = cc.GLYPHCOLLECTION_DYNAMIC
  158. ttfConfig.customGlyphs = nil
  159. ttfConfig.distanceFieldEnabled = false
  160. ttfConfig.outlineSize = 0
  161. ttfConfig.texColor = display.COLOR_BLACK
  162. editPhone:setFontConfig(ttfConfig);
  163. editPhone:setText("")
  164. editPhone:setColor(display.COLOR_BLACK)
  165. -- editPhone:setAnchorPoint(cc.p(0, 0.5))
  166. editPhone:setSize(__size)
  167. if __str then
  168. editPhone:setPlaceHolder(__str)
  169. end
  170. editPhone:setMaxLengthEnabled(true)
  171. editPhone:setMaxLength(__maxLen)
  172. return editPhone
  173. end
  174. -- 针对公告改变颜色
  175. -- <> cc.RED = cc.c3b(255,0,0)
  176. -- () cc.GREEN = cc.c3b(0,255,0)
  177. -- [] cc.BLUE = cc.c3b(0,0,255)
  178. function LabelTool.changeLabelColorEx(__label, __text)
  179. -- __text = "(ji(an)g03;V$#asahdj是手机号)犹如赌神附体,在[上海敲麻]中赢了{18}倍!"
  180. local changeTbl = {}
  181. local name = ""
  182. local namebpos
  183. -- 玩家名中可能存在[]和{} 单独拿出来处理
  184. local bp, ep, cstr = string.find(__text, "(%(.+%)+)")
  185. -- print("kkk----", bp, ep, cstr)
  186. if cstr then
  187. name = string.sub(cstr, 2, -2)
  188. local s1 = string.sub(__text, 0, bp-1)
  189. local s2 = string.sub(__text, ep+1, #__text)
  190. __text = s1 .. "%s" .. s2
  191. -- print(s1, s2, "==")
  192. namebpos = math.max(bp, 0)
  193. local posh,pose = string.find(name, ";V$#")
  194. if posh and pose then
  195. name = string.sub(name, 1, posh-1)
  196. end
  197. end
  198. for catchStr in string.gmatch(__text, '(%b[])') do
  199. local str = string.gsub(catchStr, "[%[%]]","")
  200. changeTbl[str] = GONGGAO_COLOR_GAME
  201. end
  202. for catchStr in string.gmatch(__text, '(%b{})') do
  203. local str = string.gsub(catchStr, "[{}]","")
  204. changeTbl[str] = GONGGAO_COLOR_NUM
  205. end
  206. local _text = string.gsub(__text, "[{}%[%]]", "")
  207. _text = string.format(_text, name)
  208. __label:setString(_text)
  209. -- print("changeLabelColorEx", _text)
  210. -- dump(changeTbl)
  211. local function changeColor(bpos, epos, color)
  212. if bpos and epos then
  213. if bpos ~= 0 then
  214. bpos = LabelTool.countCNWords(string.sub(_text, 0, bpos-1))
  215. end
  216. epos = LabelTool.countCNWords(string.sub(_text, 0, epos)) - 1
  217. for index = bpos, epos do
  218. local sp = __label:getLetter(index)
  219. if (sp) then
  220. sp:setColor(color)
  221. else
  222. -- print("error sp")
  223. end
  224. end
  225. end
  226. end
  227. for k, v in pairs(changeTbl) do
  228. local bpos, epos = string.find(_text, k)
  229. -- print(bpos, epos, k)
  230. changeColor(bpos, epos, v)
  231. end
  232. if namebpos then
  233. -- print(namebpos, #name, string.len(name),"000")
  234. changeColor(namebpos, namebpos+(string.len(name)-1), GONGGAO_COLOR_NAME)
  235. end
  236. end
  237. return LabelTool