SRTextHelper.lua 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. local SRTextHelper = class("SRTextHelper")
  2. function SRTextHelper:ctor()
  3. end
  4. function SRTextHelper:convertToRichText(source, defaultSize, defaultColor)
  5. local PCFontConfig = SL:GetMetaValue("GAME_DATA", "PCFontConfig")
  6. defaultSize = (PCFontConfig and PCFontConfig[2]) or defaultSize or SL:GetMetaValue("GAME_DATA", "DEFAULT_FONT_SIZE") or 16
  7. defaultColor = defaultColor or "#FFFFFF"
  8. local find_info = nil
  9. local begin_pos = 0
  10. local end_pos = 0
  11. local font_text_table = {}
  12. while string.len(source) > 0 do
  13. find_info = {string.find( source, "{(.-)|(.-)}")}
  14. begin_pos = find_info[1]
  15. end_pos = find_info[2]
  16. if begin_pos and end_pos then
  17. if begin_pos > 1 then
  18. local prefix = string.sub(source, 1, begin_pos-1)
  19. self:convertToFontText(font_text_table, prefix, nil, defaultSize, defaultColor)
  20. end
  21. self:convertToFontText(font_text_table, find_info[3], find_info[4], defaultSize, defaultColor)
  22. source = string.sub(source, end_pos + 1, string.len(source))
  23. else
  24. self:convertToFontText(font_text_table, source, nil, defaultSize, defaultColor)
  25. source = ""
  26. end
  27. end
  28. local fontText = table.concat(font_text_table, "")
  29. return fontText
  30. end
  31. function SRTextHelper:convertToFontText(font_text_table, content, attr, defaultSize, defaultColor)
  32. local styleColor = nil
  33. local styleType = nil -- 0背景 1描边
  34. if attr and string.len(attr) > 0 then
  35. local slices = string.split(attr, ':')
  36. defaultColor = GET_COLOR_BYID(tonumber(slices[1]))
  37. styleColor = tonumber(slices[2])
  38. styleType = tonumber(slices[3])
  39. end
  40. if styleType and styleType == 1 then
  41. local color = GET_COLOR_BYID(styleColor)
  42. content = string.format("<font size='%s' color='%s'><outline size='%s' color='%s'>%s</outline></font>", defaultSize, defaultColor, 1, color, content)
  43. else
  44. local PCFontConfig = SL:GetMetaValue("GAME_DATA", "PCFontConfig")
  45. content = string.format("<font size='%s' color='%s'>%s</font>", (PCFontConfig and PCFontConfig[2]) or defaultSize, defaultColor, content)
  46. end
  47. table.insert(font_text_table, content)
  48. end
  49. return SRTextHelper