Here is the main thing to do is the game bulletin inside, so I added a simple HTML <font> tag parsing, is to give the operation of small white preview, but the size of the property is not, because later there is no such demand, so it is too lazy to change.
The idea is simple: Set the line width and row height, and then add a word to it, until you reach the line width limit or line break, wrap, and then loop.
--Simple HTML label--[[only implement <font> tag for the following tags, add size and Color properties <br> Wrap--]]--[=[demo:html = "& Lt;font size=\ "50\" color=\ "#ff0000 \" > AB, Casdjfladfasdgasdasdgasdfasdfqwetqwetrtwqrtqwerqwerqwerqwetqwetqwerqwerqwer44444444444222222222asdugowe<br >iurlsdjlkasdhga </font><font size=\ "28\" color=\ "#002 \" > Ab,21<br>iurlsdjlkasdhga </font ><font size=\ "22\" color=\ "#2231 \" > ab,12<br>12 </font><font size=\ "10\" color=\ "#2202 \" > Ab,.21<br>1 </font> "Local lable = require (" Common. Htmllabel "). New (600,html) lable:setposition (400,400) self._lay:addchild (lable)--]=]local Htmllabel = Class (" HtmlLab El ", Function () return layout:create () end) function Htmllabel:ctor (width, html) self._width = width Self._height = 0 Self._defaultfontsize = Self._defaultfontcolor = 0x000000 self:initwithstring (html) endlocal Headtag = "<font .-> "Local Lasttag =" </font> "function Htmllabel:Initwithstring (HTML) self._html = string.lower (HTML) self:removeallchildrenwithcleanup (true) Local labellist = {} HTML = string.gsub (HTML, "<br>", "\ n") while String.len (HTML) > 0 do local bpos,epos = STRING.F IND (HTML,LASTTAG) Local node = {} Local Headtagendpos = 1 if not find and then local Hbpos,hep OS = String.find (html,headtag) if Hbpos then--local head = string.sub (HTML, Hbpos, (HEPOS-HBP os+1)) Local head = string.sub (HTML, hbpos+5, (hePos-1)) head = String.gsub (head, "#", " 0x ") head = String.gsub (Head," (%d\ ")", "%1,") Local Strfunc = "Return {": Head.. "}" node = Assert (LoadString (Strfunc)) () if not node.color and then Node.colo R = Self._defaultfontcolor Else Node.color = Tonumber (node.color) End If not node.size Then node.size = Self._defaultfontsize Else Node.size = Tonumber (nod e.size) End headtagendpos = Hepos + 1--HTML = string.sub (HTML, hepos+1, str Ing.len (HTML)) Else Node.color = Self._defaultfontcolor Node.size = Self._default FontSize End node.str = string.sub (html,headtagendpos,bpos-1) HTML = string.sub (HTML, EP Os+1, String.len (HTML))--Remove processed segments else Node.str = html Node.color = Self._defaultfontcolor Node.size = self._defaultfontsize html = "" End Table.insert (labellist, node) end If #labelList = = 0 Then return end local Node1 = Labellist[1] Self:initrichlabel (node1.str,self._wid Th, node1.size* (1+0.2), Node1.size, CCC3 (Math.floor (node1.color/0x10000), Math.floor (node1.color/0x100)%0x100, Node1.color% 0x100)) foR i=2, #labelList do node1 = labellist[i]--Print ("%02x%02x%02x", Math.floor (node1.color/0x10000), Math.floor (node1.color/0x100)%0x100, node1.color% 0x100) self:addstring (Node1.str, CCC3 (Math.floor (node1.color/0x10000), M Ath.floor (node1.color/0x100)%0x100, node1.color% 0x100)) endendfunction Htmllabel:initrichlabel (content, LineWidth, Lineheight, fntsize, color) self.linewidth = linewidth Self.lineheight = lineheight self._fntsize = fntSize sel F._defaultcolor = Colorself.widthindex = 0self.rowindex = 1self.nodelist = {} Self.anchorpoint = CCP (0.5,0.5) self:a Ddstring (content) end--reset a stringfunction htmllabel:setstring (content, color) self:removeallchildrenwithcleanup ( true) Self.widthindex = 0self.rowindex = 1 self.nodelist = {} Self._defaultcolor = Color or Self._defaultcolor sel F:addstring (content) end--adds a string function htmllabel:addstring (labelstr, color) color = Color or Self._defaultcolori F Self.widthindex >= self.LineWidth Thenself.widthindex = 0self.rowindex = Self.rowindex + 1endwhile labelstr ~= nil and Labelstr ~= "" Dolocal BegI Ndex,endindex = Self:getmaxsubstr (Labelstr,self.linewidth-self.widthindex, self._fntsize) If BegIndex = nil then--newline s Elf.widthindex = 0self.rowindex = self.rowindex + 1begindex,endindex = self:getmaxsubstr (LabelStr,self.lineWidth- Self.widthindex, self._fntsize) endlocal subStr = String.sub (labelstr,begindex,endindex) labelstr = String.sub ( Labelstr,endindex+1,string.len (LABELSTR)) Local label = Label:create () label:setfontsize (self._fntsize) L ocal text = string.gsub (subStr, "\ n", "")--Remove line break Label:settext (text) label:setcolor (color) Label:setpositionx (Self.widthindex) self:addchild (label) Label:setanchorpoint (CCP (0,0)) Label.rowindex = Self.rowindex Label.srcX = self . Widthindextable.insert (Self.nodelist,label) Self.widthindex = Self.widthindex + label:getcontentsize (). width if str Ing.find (subStr, ' \ n ') then--a forced line break appears Self.widThindex = 0self.rowindex = self.rowindex + 1 endend self:updateinset () endfunction htmllabel:setcontentsize (W, h) Local SZ = w if h then Sz = Ccsize (W, h) End self.contentsize = Szendfunction htmllabel:getcontents Ize () Return self.contentsizeend--update container all node position function Htmllabel:updateinset () Local row = Self.rowindexif Self.widthindex = = 0 Then--if the last line is not yet content row = Row-1end local contentsize = Ccsize (self.linewidth,row*self.lineheight) Local anchory = contentsize.height * Self.anchorpoint.y local Anchorx = contentsize.width * Self.anchorPoint.xfor k,v In pairs (self.nodelist) dov:setpositiony ((row-v.rowindex) *self.lineheight-anchory) V:setpositionx (V.srcx-ancho RX) endself:setcontentsize (contentsize) end--set anchor function Htmllabel:setanchorpoint (x, y) Local pt = Nil if not y the n pt = x Else pt = CCP (x, y) End If pt.x = = self.anchorpoint.x and Pt.y = = Self.anchorpoint.y Then Return End Self.anchorpoint = pt Self:updateinset () endfunction htmllabel:setposition (x, y) Local pt = Nil if not Y then pt = x else pt = CCP (x, y) end ccnode.setposition (self,pt) end--Gets the width range, the oldest string function Htmllabel:getmaxsubstr (_string , width,fntsize) Local result = "" Local index = 1local is_end = truelocal Index_max = String.len (_string) Local label = label : Create () label:setfontsize (fntsize) while is_end dolocal firstcase = String.byte (_string,index) If firstcase = nil th Enbreakendlocal Caselen = self:sizeof (firstcase)--character width local strcase = string.sub (_string,index,index + caseLen-1) if Strcase = = "\ n" thenresult = result: Strcaseindex = index + caselen--reserved return break endlabel:settext (Result: strcase) Local t = Label:getconte Ntsize (). Width*label:getscale () If T < width Thenresult = result: Strcaseindex = index + caselenelseis_end = FalseEnd If Index>index_max then Is_end=false end E NDIF result = = "" Thenreturn NilendrEturn 1,index-1end--Gets the number of bytes of a character function htmllabel:sizeof (firstcase) if (firstcase >= 0 and Firstcase <= 0x7f) Then return 1elseif firstcase >= 0x80 and Firstcase < 0xe0 Thenreturn 2 ElseIf firstcase >= 0xe0 then re Turn 3 EndEnd function htmllabel:getsize () return ccsize (Self._width, self._height) Endreturn Htmllabel
Cocos2dx-lua Simple font Color splicing implementation