123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202 |
- using System;
- using System.Collections.Generic;
- using System.Text;
- using System.Text.RegularExpressions;
- using UnityEngine.EventSystems;
- namespace UnityEngine.UI
- {
- [ExecuteInEditMode]
- public class HyphenationJP : UIBehaviour
- {
- private string text
- {
- get
- {
- return this._Text.text;
- }
- }
- private RectTransform _RectTransform
- {
- get
- {
- if (this._rectTransform == null)
- {
- this._rectTransform = base.GetComponent<RectTransform>();
- }
- return this._rectTransform;
- }
- }
- private Text _Text
- {
- get
- {
- if (this._text == null)
- {
- this._text = base.GetComponent<Text>();
- }
- return this._text;
- }
- }
- protected override void OnRectTransformDimensionsChange()
- {
- base.OnRectTransformDimensionsChange();
- if (this._Text == null)
- {
- return;
- }
- this.UpdateText(this.text);
- }
- public void UpdateText()
- {
- this.UpdateText(this.text);
- }
- private void UpdateText(string str)
- {
- this._Text.text = this.GetFormatedText(this._Text, str);
- }
- private float GetTextWidth(Text textComp, string message)
- {
- if (this._Text.supportRichText)
- {
- message = HyphenationJP.REG_RICH_TEXT.Replace(message, string.Empty);
- }
- textComp.text = message;
- return textComp.preferredWidth;
- }
- private string GetFormatedText(Text textComp, string msg)
- {
- if (!base.enabled)
- {
- return msg;
- }
- if (string.IsNullOrEmpty(msg))
- {
- return string.Empty;
- }
- float width = this._RectTransform.rect.width;
- float textWidth = this.GetTextWidth(textComp, " ");
- textComp.horizontalOverflow = HorizontalWrapMode.Overflow;
- StringBuilder stringBuilder = new StringBuilder();
- float num = 0f;
- foreach (string text in this.GetWordList(msg))
- {
- num += this.GetTextWidth(textComp, text);
- if (HyphenationJP.IsNewLine(text))
- {
- num = 0f;
- }
- else
- {
- if (text == " ")
- {
- num += textWidth;
- }
- if (num > width)
- {
- stringBuilder.Append(Environment.NewLine);
- num = this.GetTextWidth(textComp, text);
- }
- }
- stringBuilder.Append(text);
- }
- return stringBuilder.ToString();
- }
- private List<string> GetWordList(string tmpText)
- {
- List<string> list = new List<string>();
- StringBuilder stringBuilder = new StringBuilder();
- char c = '\0';
- bool flag = false;
- for (int i = 0; i < tmpText.Length; i++)
- {
- char c2 = tmpText[i];
- char c3 = (i >= tmpText.Length - 1) ? c : tmpText[i + 1];
- char c4 = (i <= 0) ? c : tmpText[i - 1];
- stringBuilder.Append(c2);
- if (c2 == '<')
- {
- flag = true;
- }
- else if (c2 == '>')
- {
- flag = false;
- }
- if (!flag && (HyphenationJP.IsNewLine(c2.ToString()) || (HyphenationJP.IsLatin(c2) && HyphenationJP.IsLatin(c4) && HyphenationJP.IsLatin(c2) && !HyphenationJP.IsLatin(c4)) || (!HyphenationJP.IsLatin(c2) && HyphenationJP.CHECK_HYP_BACK(c4)) || (!HyphenationJP.IsLatin(c3) && !HyphenationJP.CHECK_HYP_FRONT(c3) && !HyphenationJP.CHECK_HYP_BACK(c2)) || i == tmpText.Length - 1))
- {
- list.Add(stringBuilder.ToString());
- stringBuilder = new StringBuilder();
- }
- }
- return list;
- }
- public float textWidth
- {
- get
- {
- return this._RectTransform.rect.width;
- }
- set
- {
- this._RectTransform.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, value);
- }
- }
- public int fontSize
- {
- get
- {
- return this._Text.fontSize;
- }
- set
- {
- this._Text.fontSize = value;
- }
- }
- private static bool CHECK_HYP_FRONT(char c)
- {
- return HyphenationJP.HYP_FRONT_.Contains(c);
- }
- private static bool CHECK_HYP_BACK(char c)
- {
- return HyphenationJP.HYP_BACK_.Contains(c);
- }
- private static bool IsLatin(char c)
- {
- return HyphenationJP.HYP_LATIN_.Contains(c);
- }
- private static bool IsNewLine(string s)
- {
- return HyphenationJP.REG_NEW_LINE.IsMatch(s);
- }
- private RectTransform _rectTransform;
- private Text _text;
- private static readonly string RICH_TEXT_REPLACE = "(<color=.*?>|</color>|<size=.*?>|</size>|<b>|</b>|<i>|</i>|<a href=.*?>|</a>)";
- private static readonly HashSet<char> HYP_FRONT_ = new HashSet<char>(",)]}、。)〕〉》」』】〙〗〟’”⦆»ァィゥェォッャュョヮヵヶっぁぃぅぇぉっゃゅょゎ‐゠–〜ー?!!?‼⁇⁈⁉・:;。.".ToCharArray());
- private static readonly HashSet<char> HYP_BACK_ = new HashSet<char>("(([{〔〈《「『【〘〖〝‘“⦅«".ToCharArray());
- private static readonly HashSet<char> HYP_LATIN_ = new HashSet<char>("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789<>=/().,#\"'".ToCharArray());
- private static readonly Regex REG_NEW_LINE = new Regex("\r\n|\r|\n");
- private static readonly Regex REG_RICH_TEXT = new Regex(HyphenationJP.RICH_TEXT_REPLACE, RegexOptions.IgnoreCase);
- }
- }
|