UITextStyleManager.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. using HDJ.Framework.Utils;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using UnityEngine;
  7. using UnityEngine.UI;
  8. public class UITextStyleManager
  9. {
  10. private const string FilePathDir = "Assets/Resources/Config/";
  11. private const string FileName = "UITextStyleConfig";
  12. public static Dictionary<string, Dictionary<SystemLanguage, TextStyleData>> styleDataDic = new Dictionary<string, Dictionary<SystemLanguage, TextStyleData>>();
  13. public static void Init()
  14. {
  15. styleDataDic = LoadData();
  16. }
  17. public static Dictionary<string, Dictionary<SystemLanguage, TextStyleData>> LoadData()
  18. {
  19. string text = "";
  20. if (Application.isPlaying)
  21. {
  22. text = AssetsPoolManager.ReadTextFile(FileName);
  23. }
  24. else
  25. {
  26. text = FileUtils.LoadTextFileByPath(FilePathDir + FileName + ".txt");
  27. }
  28. if (!string.IsNullOrEmpty(text))
  29. {
  30. return JsonUtils.FromJson<Dictionary<string, Dictionary<SystemLanguage, TextStyleData>>>(text);
  31. }
  32. else
  33. {
  34. return new Dictionary<string, Dictionary<SystemLanguage, TextStyleData>>();
  35. }
  36. }
  37. public static void SaveData(Dictionary<string, Dictionary<SystemLanguage, TextStyleData>> styleDataDic)
  38. {
  39. string text = JsonUtils.ToJson(styleDataDic);
  40. FileUtils.CreateTextFile(FilePathDir + FileName + ".txt", text);
  41. }
  42. public static bool ContainsData(string name, SystemLanguage language)
  43. {
  44. Dictionary<SystemLanguage, TextStyleData> data = null;
  45. if (styleDataDic.ContainsKey(name))
  46. data = styleDataDic[name];
  47. else
  48. return false;
  49. if (data.ContainsKey(language))
  50. {
  51. return true;
  52. }
  53. else
  54. return false;
  55. }
  56. public static TextStyleData GetTextStyleData(string name,SystemLanguage language)
  57. {
  58. Dictionary<SystemLanguage, TextStyleData> data = null;
  59. if (styleDataDic.ContainsKey(name))
  60. data = styleDataDic[name];
  61. else
  62. {
  63. Debug.LogError("no TextStyleData name:" + name);
  64. return null;
  65. }
  66. if (data.ContainsKey(language))
  67. {
  68. return data[language];
  69. }
  70. else
  71. {
  72. Debug.LogError("no TextStyleData language:" + language);
  73. return null;
  74. }
  75. }
  76. public static void SetText(Text text, string name, SystemLanguage language)
  77. {
  78. if(ContainsData(name,language))
  79. {
  80. TextStyleData data = GetTextStyleData(name, language);
  81. if (!ResourcesConfigManager.GetIsExitRes(data.fontName))
  82. {
  83. Debug.LogError("dont find font :" + data.fontName);
  84. }
  85. else
  86. text.font = AssetsPoolManager.Load<Font>(data.fontName);
  87. text.fontSize = data.fontSize;
  88. text.fontStyle = data.fontStyle;
  89. text.resizeTextForBestFit = data.bestFit;
  90. text.resizeTextMinSize = data.minSize;
  91. text.resizeTextMaxSize = data.maxSize;
  92. text.alignment = data.alignment;
  93. text.supportRichText = data.richText;
  94. text.horizontalOverflow = data.horizontalOverflow;
  95. text.verticalOverflow = data.verticalOverflow;
  96. text.lineSpacing = data.lineSpacing;
  97. }
  98. }
  99. public static void SetText(Text text, TextStyleData data)
  100. {
  101. if (!ResourcesConfigManager.GetIsExitRes(data.fontName))
  102. {
  103. Debug.LogError("dont find font :" + data.fontName);
  104. }
  105. else
  106. text.font = AssetsPoolManager.Load<Font>(data.fontName);
  107. text.fontSize = data.fontSize;
  108. text.fontStyle = data.fontStyle;
  109. text.resizeTextForBestFit = data.bestFit;
  110. text.resizeTextMinSize = data.minSize;
  111. text.resizeTextMaxSize = data.maxSize;
  112. text.alignment = data.alignment;
  113. text.supportRichText = data.richText;
  114. text.horizontalOverflow = data.horizontalOverflow;
  115. text.verticalOverflow = data.verticalOverflow;
  116. text.lineSpacing = data.lineSpacing;
  117. }
  118. public static TextStyleData GetTextStyleDataFromText(Text text)
  119. {
  120. TextStyleData data = new TextStyleData();
  121. data.fontName = text.font.name;
  122. data.fontSize=text.fontSize ;
  123. data.fontStyle=text.fontStyle ;
  124. data.bestFit=text.resizeTextForBestFit ;
  125. data.minSize=text.resizeTextMinSize ;
  126. data.maxSize=text.resizeTextMaxSize ;
  127. data.alignment=text.alignment ;
  128. data.richText=text.supportRichText ;
  129. data.horizontalOverflow=text.horizontalOverflow ;
  130. data.verticalOverflow=text.verticalOverflow ;
  131. data.lineSpacing=text.lineSpacing ;
  132. return data;
  133. }
  134. }
  135. public class TextStyleData
  136. {
  137. public string fontName { get; set; }
  138. //
  139. // 摘要:
  140. // Font size.
  141. public int fontSize { get; set; }
  142. //
  143. // 摘要:
  144. // Font Style.
  145. public FontStyle fontStyle { get; set; }
  146. //
  147. // 摘要:
  148. // Is best fit used.
  149. public bool bestFit { get; set; }
  150. //
  151. // 摘要:
  152. // Minimum text size.
  153. public int minSize { get; set; }
  154. //
  155. // 摘要:
  156. // Maximum text size.
  157. public int maxSize { get; set; }
  158. //
  159. // 摘要:
  160. // How is the text aligned.
  161. public TextAnchor alignment { get; set; }
  162. //
  163. // 摘要:
  164. // Use the extents of glyph geometry to perform horizontal alignment rather than
  165. // glyph metrics.
  166. public bool alignByGeometry { get; set; }
  167. //
  168. // 摘要:
  169. // Should RichText be used?
  170. public bool richText { get; set; }
  171. //
  172. // 摘要:
  173. // Horizontal overflow mode.
  174. public HorizontalWrapMode horizontalOverflow { get; set; }
  175. //
  176. // 摘要:
  177. // Vertical overflow mode.
  178. public VerticalWrapMode verticalOverflow { get; set; }
  179. //
  180. // 摘要:
  181. // Line spacing.
  182. public float lineSpacing { get; set; }
  183. }