void DisposeText() { _hrefInfo = null; DisposeOutPut(); DisposeEmoji(); DisposeHref(); }
// 获取超链接解析后的最后输出文本 protected virtual string GetOutputText(string outputText) { s_TextBuilder.Length = 0; m_HrefInfos.Clear(); var indexText = 0; foreach (Match match in s_HrefRegex.Matches(outputText)) { s_TextBuilder.Append(outputText.Substring(indexText, match.Index - indexText)); Newvaule.Add(match.Groups[2].ToString()); var group = match.Groups[1]; var hrefInfo = new HrefInfo { startIndex = s_TextBuilder.Length * 4, // 超链接里的文本起始顶点索引 endIndex = (s_TextBuilder.Length + match.Groups[2].Length - 1) * 4 + 3, name = group.Value }; m_HrefInfos.Add(hrefInfo); s_TextBuilder.Append(match.Groups[2].Value); indexText = match.Index + match.Length; } s_TextBuilder.Append(outputText.Substring(indexText, outputText.Length - indexText)); if (m_HrefInfos.Count > 0) { transform.GetComponentDefault <Chatclick>(); } return(s_TextBuilder.ToString()); }
/// <summary> /// 获取超链接解析后的最后输出文本 /// </summary> /// <returns></returns> protected string GetOutputText() { s_TextBuilder.Length = 0; m_HrefInfos.Clear(); var indexText = 0; foreach (Match match in s_HrefRegex.Matches(text)) { s_TextBuilder.Append(text.Substring(indexText, match.Index - indexText)); s_TextBuilder.Append("<color=blue>"); // 超链接颜色 var group = match.Groups[1]; var hrefInfo = new HrefInfo { startIndex = s_TextBuilder.Length * 4, // 超链接里的文本起始顶点索引 endIndex = (s_TextBuilder.Length + match.Groups[2].Length - 1) * 4 + 3, name = group.Value }; m_HrefInfos.Add(hrefInfo); s_TextBuilder.Append(match.Groups[2].Value); s_TextBuilder.Append("</color>"); indexText = match.Index + match.Length; } s_TextBuilder.Append(text.Substring(indexText, text.Length - indexText)); return(s_TextBuilder.ToString()); }
void DisposeHref() { //_outputText内容示例:看我的装备吧<quad name=111 size=30 wide=1 /><color=#11111111>[装备]</color><quad name=123 size=30 wide=1 /><quad name=333 size=30 wide=1 /><href=<color=#11111111>[装备]</color> tpv=0,11111,22222 /> _hrefInfo = null; string tempText = string.Copy(_outputText); foreach (Match match in HrefRegex.Matches(_outputText)) { //填充超链接信息 int index = _outputText.IndexOf(match.Groups[1].Value); int hrefStartIndex = index * 4; int hrefEndIndex = (index + match.Groups[1].Value.Length) * 4 - 1; tempText = tempText.Remove(tempText.IndexOf(match.Groups[0].Value), match.Groups[0].Value.Length); string name = match.Groups[1].Value; _hrefInfo = new HrefInfo { startIndex = hrefStartIndex, // 超链接里的文本起始顶点索引 endIndex = hrefEndIndex, name = name, hrefStr = match.Groups[2].Value }; } _outputText = tempText; }
/// <summary> /// 获取超链接解析后的最后输出文本 @"<a href=([^>\n\s]+)>(.*?)(</a>)" /// </summary> /// <returns></returns> protected virtual string GetOutputText(string outputText) { s_TextBuilder.Length = 0; m_HrefInfos.Clear(); var indexText = 0; foreach (Match match in s_HrefRegex.Matches(outputText)) { s_TextBuilder.Append(outputText.Substring(indexText, match.Index - indexText)); //s_TextBuilder.Append("<color=blue>"); // 超链接颜色 var tmp = color; var group = match.Groups[1]; var hrefInfo = new HrefInfo { startIndex = s_TextBuilder.Length * 4, // 超链接里的文本起始顶点索引 endIndex = (s_TextBuilder.Length + match.Groups[3].Length - 1) * 4 + 3, name = group.Value, hasUnderline = match.Groups[2].Value == "1", //color = HexToColor("#00ffff") color = tmp }; m_HrefInfos.Add(hrefInfo); s_TextBuilder.Append(match.Groups[3].Value); //s_TextBuilder.Append("</color>"); indexText = match.Index + match.Length; } s_TextBuilder.Append(outputText.Substring(indexText, outputText.Length - indexText)); return(s_TextBuilder.ToString()); }
/// <summary> /// 获取超链接解析后的文本。 /// </summary> /// <returns>传给控件显示的文本。</returns> protected string GetOutputText() { StringBuilder sb = new StringBuilder(); string txt = text; mHrefInfos.Clear(); var indexText = 0; foreach (Match match in HrefRegex.Matches(text)) { sb.Append(txt.Substring(indexText, match.Index - indexText)); //sb.Append("<color=#0000FF>"); // 超链接颜色 var hrefInfo = new HrefInfo(); hrefInfo.startIndex = sb.Length * 6; // 超链接里的文本起始顶点索引 hrefInfo.endIndex = (sb.Length + match.Groups[2].Length - 1) * 6 + 5; hrefInfo.param = match.Groups[1].Value.Trim(); hrefInfo.color = this.color; mHrefInfos.Add(hrefInfo); sb.Append(match.Groups[2].Value); //sb.Append("</color>"); indexText = match.Index + match.Length; } sb.Append(txt.Substring(indexText, txt.Length - indexText)); return(sb.ToString()); }
/// <summary> /// 获取超链接解析后的最后输出文本 /// </summary> /// <returns></returns> protected virtual string CollectHref(string outputText) { s_TextBuilder.Length = 0; m_HrefInfos.Clear(); var indexText = 0; m_HrefRegex.ForEach(i => { foreach (Match match in Regex.Matches(outputText, i, RegexOptions.Singleline)) { var sub = outputText.Substring(indexText, match.Index - indexText); s_TextBuilder.Append(sub); s_TextBuilder.Append($"<a href=href{indexText}><color=#00ff00>"); // 超链接颜色 var href = match.Groups[1]; var hrefInfo = new HrefInfo { startIndex = s_TextBuilder.Length * 4, // 超链接里的文本起始顶点索引//match.Index*4, // endIndex = (s_TextBuilder.Length + match.Groups[1].Length - 1) * 4 + 3, name = href.Value }; m_HrefInfos.Add(hrefInfo); s_TextBuilder.Append(match.Groups[1].Value); s_TextBuilder.Append("</color></a>"); indexText = match.Index + match.Length; } }); s_TextBuilder.Append(outputText.Substring(indexText, outputText.Length - indexText)); return(s_TextBuilder.ToString()); }
private string GetOutputText() { _imagesTagInfoList.Clear(); IList <UIVertex> verts = cachedTextGenerator.verts; StringBuilder textBuilder = new StringBuilder(); int textIndex = 0; foreach (Match match in _inputRegex.Matches(text)) { int tempId = 0; if (!string.IsNullOrEmpty(match.Groups[1].Value) && !match.Groups[1].Value.Equals("-")) { tempId = int.Parse(match.Groups[1].Value); } string tempTag = match.Groups[2].Value; //更新超链接 if (tempId < 0) { textBuilder.Append(text.Substring(textIndex, match.Index - textIndex)); textBuilder.Append("<color=blue>"); int _startIndex = textBuilder.Length * 4; textBuilder.Append("[" + match.Groups[2].Value + "]"); int _endIndex = textBuilder.Length * 4 - 2; textBuilder.Append("</color>"); var hrefInfo = new HrefInfo { id = Mathf.Abs(tempId), startIndex = _startIndex, // 超链接里的文本起始顶点索引 endIndex = _endIndex, name = match.Groups[2].Value }; _listHrefInfos.Add(hrefInfo); } //更新表情 else { textBuilder.Append(text.Substring(textIndex, match.Index - textIndex)); SpriteAsset spriteAsset = SpriteFaceCache.GetAsset(tempId); SpriteInforGroup tempGroup = SpriteFaceCache.GetAsset(tempId, tempTag); float imgSize = spriteAsset == null ? 24.0f : spriteAsset.size; float imgWidth = spriteAsset == null ? 1.0f : spriteAsset.width; int vertexIndex = textBuilder.Length * 4; textBuilder.Append(@"<quad size=" + imgSize + " width=" + imgWidth + " />"); SpriteTagInfo spInfo = new SpriteTagInfo(); spInfo.vertId = vertexIndex; spInfo.faceId = tempId; spInfo.action = tempTag; spInfo.size = new Vector2(imgSize * imgWidth, imgSize); _imagesTagInfoList.Add(spInfo); } textIndex = match.Index + match.Length; } textBuilder.Append(text.Substring(textIndex, text.Length - textIndex)); return(textBuilder.ToString()); }
protected virtual string GetOutputText(string outputText) { s_TextBuilder.Length = 0; m_HrefInfos.Clear(); var indexText = 0; foreach (Match match in s_HrefRegex.Matches(outputText)) { s_TextBuilder.Append(outputText.Substring(indexText, match.Index - indexText)); s_TextBuilder.Append("<color=#" + urlColor + ">"); var group = match.Groups[1]; var hrefInfo = new HrefInfo { startIndex = s_TextBuilder.Length * 4, endIndex = (s_TextBuilder.Length + match.Groups[2].Length - 1) * 4 + 3, name = group.Value }; m_HrefInfos.Add(hrefInfo); s_TextBuilder.Append(match.Groups[2].Value); s_TextBuilder.Append("</color>"); indexText = match.Index + match.Length; } s_TextBuilder.Append(outputText.Substring(indexText, outputText.Length - indexText)); return(s_TextBuilder.ToString()); }
protected string GetOutputText() { textBuilder.Length = 0; var indexText = 0; fixedString = this.text; if (inspectorIconList != null && inspectorIconList.Length > 0) { foreach (IconName icon in inspectorIconList) { if (icon.name != null && icon.name != "") { fixedString = fixedString.Replace(icon.name, "<quad name=" + icon.name + " size=" + fontSize + " width=1 />"); } } } int count = 0; foreach (Match match in hrefRegex.Matches(fixedString)) { textBuilder.Append(fixedString.Substring(indexText, match.Index - indexText)); textBuilder.Append("<color=" + HyperlinkColor + ">"); // Hyperlink color var group = match.Groups[1]; if (isCreatingHrefInfos) { var hrefInfo = new HrefInfo { startIndex = textBuilder.Length * 4, // Hyperlinks in text starting vertex indices endIndex = (textBuilder.Length + match.Groups[2].Length - 1) * 4 + 3, name = group.Value }; hrefInfos.Add(hrefInfo); } else { if (hrefInfos.Count > 0) { hrefInfos[count].startIndex = textBuilder.Length * 4; // Hyperlinks in text starting vertex indices; hrefInfos[count].endIndex = (textBuilder.Length + match.Groups[2].Length - 1) * 4 + 3; count++; } } textBuilder.Append(match.Groups[2].Value); textBuilder.Append("</color>"); indexText = match.Index + match.Length; } // we should create array only once or if there is any change in the text if (isCreatingHrefInfos) { isCreatingHrefInfos = false; } textBuilder.Append(fixedString.Substring(indexText, fixedString.Length - indexText)); return(textBuilder.ToString()); }
/// <summary> /// 调整下划线。 /// </summary> /// <param name="vertices">顶点数组。</param> /// <param name="lineuv">下划线的纹理坐标。</param> private void ModifyHrefMesh(List <UIVertex> vertices, Vector2 lineuv) { Vector3[] linev = new Vector3[6]; for (int i = 0; i < m_HrefInfos.Count; ++i) { HrefInfo info = m_HrefInfos[i]; int start = info.StartIndex * 6; int end = Math.Min(info.EndIndex * 6, vertices.Count); if (start >= vertices.Count) { break; } UIVertex startuiv = vertices[start]; Bounds bounds = new Bounds(startuiv.position, Vector3.zero); info.Boxes.Clear(); for (int j = start + 1; j < end; ++j) { Vector3 pos = vertices[j].position; if (j % 6 == 0 && pos.y < bounds.min.y) // 换行重新添加包围框,每个字符6个点 { info.Boxes.Add(new Rect(bounds.min, bounds.size)); bounds = new Bounds(pos, Vector3.zero); } else { bounds.Encapsulate(pos); // 扩展包围框 } } info.Boxes.Add(new Rect(bounds.min, bounds.size)); //在末尾添加下划线顶点,下划线颜色等于线条第一个字符的颜色 for (int j = 0; j < info.Boxes.Count; ++j) { Rect r = info.Boxes[j]; float xl = r.min.x; float xr = r.max.x; float yt = r.min.y; float yb = yt - m_UnderLineHeight; linev[0] = new Vector3(xl, yt); linev[1] = new Vector3(xr, yt); linev[2] = new Vector3(xr, yb); linev[3] = new Vector3(xr, yb); linev[4] = new Vector3(xl, yb); linev[5] = new Vector3(xl, yt); for (int k = 0; k < linev.Length; ++k) { UIVertex uiv = startuiv; uiv.uv0 = lineuv; uiv.position = linev[k]; vertices.Add(uiv); } } } }
/// <summary> /// 获取超链接解析后的最后输出文本 /// </summary> /// <returns></returns> protected virtual string GetOutputText(string outputText) { if (!UseHint) { return(outputText); } s_TextBuilder.Length = 0; m_HrefInfos.Clear(); var indexText = 0; //foreach (Match match in s_HrefRegex.Matches(outputText)) //{ // s_TextBuilder.Append(outputText.Substring(indexText, match.Index - indexText)); // //s_TextBuilder.Append("<color='#9ed7ff'>"); // 超链接颜色ff6600 // var group = match.Groups[1]; // var hrefInfo = new HrefInfo // { // startIndex = s_TextBuilder.Length * 4, // 超链接里的文本起始顶点索引 // endIndex = (s_TextBuilder.Length + match.Groups[2].Length - 1) * 4 + 3, // name = group.Value // }; // m_HrefInfos.Add(hrefInfo); // s_TextBuilder.Append(match.Groups[2].Value); // //s_TextBuilder.Append("</color>"); // indexText = match.Index + match.Length; //} //s_TextBuilder.Length = 0; //m_HrefInfos.Clear(); //indexText = 0; List <HrefInfoMatch> hrefInfos = MatchHrefInfo(outputText); int length = hrefInfos.Count; for (int i = 0; i < length; i++) { s_TextBuilder.Append(outputText.Substring(indexText, hrefInfos[i].Index - indexText)); var hrefInfo = new HrefInfo { startIndex = s_TextBuilder.Length * 4, // 超链接里的文本起始顶点索引 endIndex = (s_TextBuilder.Length + hrefInfos[i].Length - 1) * 4 + 3, name = hrefInfos[i].HrefIdx }; m_HrefInfos.Add(hrefInfo); s_TextBuilder.Append(hrefInfos[i].Value); //s_TextBuilder.Append("</color>"); indexText = hrefInfos[i].Index + hrefInfos[i].Strlen; } s_TextBuilder.Append(outputText.Substring(indexText, outputText.Length - indexText)); return(s_TextBuilder.ToString()); }
private void BuildHrefInfo(HyperText text, string name, string color, bool isLink) { // 超链接颜色 m_TextBuilder.AppendFormat("<color={0}>", color); var hrefInfo = new HrefInfo { startIndex = m_TextBuilder.Length * 4, // 超链接里的文本起始顶点索引 endIndex = (m_TextBuilder.Length + name.Length - 1) * 4 + 3, text = text, isLink = isLink }; m_HrefInfos.Add(hrefInfo); m_TextBuilder.Append(name); m_TextBuilder.Append("</color>"); }
/// <summary> /// 获取超链接解析后的最后输出文本 /// </summary> /// <returns></returns> protected string GetOutputText() { textBuilder.Length = 0; var indexText = 0; if (this.clickHrefFlag) { var indexHrefInfo = 0; foreach (Match match in hrefRegex.Matches(this.text)) { var hrefInfo = this.hrefInfos[indexHrefInfo]; textBuilder.Append(this.text.Substring(indexText, match.Index - indexText)); textBuilder.Append(string.Format("<color={0}>", hrefInfo.HrefColor)); // 超链接颜色 textBuilder.Append(match.Groups[2].Value); textBuilder.Append("</color>"); indexText = match.Index + match.Length; indexHrefInfo++; } } else { this.hrefInfos.Clear(); this.removeHrefTexts.Clear(); foreach (Match match in hrefRegex.Matches(this.text)) { textBuilder.Append(this.text.Substring(indexText, match.Index - indexText)); textBuilder.Append(string.Format("<color={0}>", this.OriginalHrefColor)); // 超链接颜色 var group = match.Groups[1]; var hrefInfo = new HrefInfo(this) { startIndex = textBuilder.Length * 4, // 超链接里的文本起始顶点索引 endIndex = (textBuilder.Length + match.Groups[2].Length - 1) * 4 + 3, name = group.Value }; this.hrefInfos.Add(hrefInfo); textBuilder.Append(match.Groups[2].Value); textBuilder.Append("</color>"); indexText = match.Index + match.Length; this.removeHrefTexts.Add(new RemoveText(match.Groups[0].Value, match.Groups[2].Value)); } } textBuilder.Append(text.Substring(indexText, text.Length - indexText)); return(textBuilder.ToString()); }
protected override void OnPopulateMesh(VertexHelper vh) { base.OnPopulateMesh(vh); Vector2 extents = rectTransform.rect.size; var settings = GetGenerationSettings(extents); float unitsPerPixel = 1 / pixelsPerUnit; float fontRealSize = fontSize * pixelsPerUnit; cachedTextGenerator.Populate(this.text, settings); var vbo = cachedTextGenerator.verts; HrefInfo value = null; for (int k = 0; k < this.HrefList.Count; k++) { value = this.HrefList[k]; value.bounds.Clear(); if (value.StartIndex < vbo.Count) { Vector3 position = vbo[value.StartIndex].position; Bounds bounds = new Bounds(position, Vector3.zero); int l = value.StartIndex; int endIndex = value.EndIndex; while (l < endIndex) { if (l >= vbo.Count) { break; } position = vbo[l].position; if (position.x < bounds.min.x) { this.HrefList[k].bounds.Add(new Rect(bounds.min.x, bounds.min.y, bounds.size.x, bounds.size.y)); bounds = new Bounds(position, Vector3.zero); } else { bounds.Encapsulate(position); } l++; } this.HrefList[k].bounds.Add(new Rect(bounds.min.x, bounds.min.y, bounds.size.x, bounds.size.y)); } } }
protected HrefInfo GetHrefInsidePosition(Vector2 p_localPosition) { HrefInfo v_hrefInfoSelected = null; foreach (var hrefInfo in m_HrefInfos) { var boxes = hrefInfo.boxes; for (var i = 0; i < boxes.Count; ++i) { if (boxes[i].Contains(p_localPosition)) { v_hrefInfoSelected = hrefInfo; break; } } } return(v_hrefInfoSelected); }
private string GetLinkedText(string text) { sb.Length = 0; m_HrefInfos.clear(); var indexText = 0; Color32 defautcolor = m_LinkDefautColor; defautcolor.a = 0xff; string colorprefix = string.Format("<color=#{0:x2}{1:x2}{2:x2}>", defautcolor.r, defautcolor.g, defautcolor.b); string colorappendix = "</color>"; foreach (Match match in s_HrefRegex.Matches(text)) { sb.Append(text.Substring(indexText, match.Index - indexText)); bool bUnderLine = true; if (match.Groups[1].Value.Equals("noline")) { bUnderLine = false; } string content = match.Groups[2].Value; if (content.Length > 0) { sb.Append(colorprefix); // 超链接颜色 var hrefInfo = new HrefInfo { startpos_linkedtext = sb.Length, //一次处理后的文本起始索引 startIndex = sb.Length * 4, // 相应的顶点索引 endIndex = (sb.Length + content.Length - 1) * 4 + 3, bUnderline = bUnderLine, content = content, parameter = match.Groups[3].Value }; m_HrefInfos.add(hrefInfo); sb.Append(content); sb.Append(colorappendix); } indexText = match.Index + match.Length; } sb.Append(text.Substring(indexText, text.Length - indexText)); //print(string.Format("found {0} links",m_HrefInfos.Count)); return(sb.ToString()); }
} // end OnPopulateMesh /// <summary> /// 获取超链接解析后的最后输出文本 /// </summary> /// <returns></returns> protected virtual string GetOutputText(string outputText) { s_TextBuilder.Length = 0; m_HrefInfos.Clear(); int indexText = 0; foreach (Match match in s_HrefRegex.Matches(outputText)) { s_TextBuilder.Append(outputText.Substring(indexText, match.Index - indexText)); s_TextBuilder.Append("<color=blue>"); // 超链接颜色 HrefInfo hrefInfo = new HrefInfo { startIndex = s_TextBuilder.Length * 4, // 超链接里的文本起始顶点索引 endIndex = (s_TextBuilder.Length + match.Groups[2].Length - 1) * 4 + 3, name = match.Groups[1].Value }; m_HrefInfos.Add(hrefInfo); s_TextBuilder.Append(match.Groups[2].Value); s_TextBuilder.Append("</color>"); indexText = match.Index + match.Length; } // end foreach s_TextBuilder.Append(outputText.Substring(indexText, outputText.Length - indexText)); return s_TextBuilder.ToString(); } // end GetOutputText
/// <summary> /// Finally, the output text hyperlinks get parsed /// </summary> /// <returns></returns> protected string GetOutputText() { s_TextBuilder.Length = 0; m_HrefInfos.Clear(); var indexText = 0; fixedString = this.text; if (inspectorIconList != null && inspectorIconList.Length > 0) { foreach (IconName icon in inspectorIconList) { if (icon.name != null && icon.name != "") { fixedString = fixedString.Replace(icon.name, "<quad name=" + icon.name + " size=" + fontSize + " width=1 />"); } } } foreach (Match match in s_HrefRegex.Matches(fixedString)) { s_TextBuilder.Append(fixedString.Substring(indexText, match.Index - indexText)); s_TextBuilder.Append("<color=" + hyperlinkColor + ">"); // Hyperlink color var group = match.Groups[1]; var hrefInfo = new HrefInfo { startIndex = s_TextBuilder.Length * 4, // Hyperlinks in text starting vertex indices endIndex = (s_TextBuilder.Length + match.Groups[2].Length - 1) * 4 + 3, name = group.Value }; m_HrefInfos.Add(hrefInfo); s_TextBuilder.Append(match.Groups[2].Value); s_TextBuilder.Append("</color>"); indexText = match.Index + match.Length; } s_TextBuilder.Append(fixedString.Substring(indexText, fixedString.Length - indexText)); return(s_TextBuilder.ToString()); }
/// <summary> /// 获取超链接解析后的最后输出文本 /// </summary> /// <returns></returns> protected string GetHrefRichText() { //text = text.Replace("\n<quad","<quad"); s_TextBuilder.Length = 0; m_HrefInfos.Clear(); var indexText = 0; MatchCollection hrefMatchCollection = s_HrefRegex.Matches(text); int matchLength = 0; int hrefNameLength = 0; foreach (Match match in hrefMatchCollection) { s_TextBuilder.Append(text.Substring(indexText, match.Index - indexText)); s_TextBuilder.Append(string.Format("<color={0}>", match.Groups[4].Value));// 超链接颜色 var group = match.Groups[1]; var show = match.Groups[2]; var hrefInfo = new HrefInfo { startIndex = s_TextBuilder.Length * 4, // 超链接里的文本起始顶点索引 endIndex = (s_TextBuilder.Length + show.Length) * 4 - 1, useLine = match.Groups[3].Value == "1", name = group.Value }; m_HrefInfos.Add(hrefInfo); s_TextBuilder.Append(show.Value); s_TextBuilder.Append("</color>"); indexText = match.Index + match.Length; hrefNameLength += show.Length; matchLength += match.Length; } mRichTextParams[2] = hrefNameLength; mRichTextParams[0] = this.text.Length - matchLength + mRichTextParams[2]; s_TextBuilder.Append(text.Substring(indexText, text.Length - indexText)); return(s_TextBuilder.ToString()); }
/// <summary> /// 解析超链接值。 /// </summary> /// <returns>解析后的字符串。</returns> public string ParseHref(string text) { int index = 0; m_HrefInfos.Clear(); CacheSB.Length = 0; foreach (Match match in HrefRegex.Matches(text)) { string param = match.Groups[1].Value.Trim(); string innertext = match.Groups[2].Value; int cut = match.Length - innertext.Length; //匹配调整后缩短的长度 HrefInfo info = new HrefInfo(); CacheSB.Append(text.Substring(index, match.Index - index)); //匹配目标前的那一部分 info.StartIndex = CacheSB.Length; info.EndIndex = info.StartIndex + innertext.Length; info.Param = param; m_HrefInfos.Add(info); CacheSB.Append(innertext); index = match.Index + match.Length; AdujstIndex(m_ColorInfos, info.StartIndex, info.StartIndex + match.Length, cut, 7); //7为"</href>"标签的长度 } CacheSB.Append(text.Substring(index, text.Length - index)); return(CacheSB.ToString()); }
private void BuildHrefInfo(HyperText text, string name, string color, bool isLink) { bool includeColor = !string.IsNullOrEmpty(color); // 超链接颜色 if (includeColor) { m_TextBuilder.AppendFormat("<color=#{0}>", color); } var hrefInfo = new HrefInfo { startIndex = m_TextBuilder.Length * 4, // 超链接里的文本起始顶点索引 endIndex = (m_TextBuilder.Length + name.Length - 1) * 4 + 3, text = text, isLink = isLink }; m_HrefInfos.Add(hrefInfo); m_TextBuilder.Append(name); if (includeColor) { m_TextBuilder.Append("</color>"); } }
public void SetEmojiText(string emojiText) { mHrefInfoDic.Clear(); mEmojiDic.Clear(); int lastSubIndex = 0; StringBuilder result = new StringBuilder(); if (emojiText.IndexOf("href") != -1) { var hrefMatches = s_HrefRegex.Matches(emojiText); Match hrefMatchItem = null; for (int i = 0; i < hrefMatches.Count; i++) { hrefMatchItem = hrefMatches[i]; HrefInfo info = new HrefInfo(); info.link = hrefMatchItem.Groups[1].Value; info.text = hrefMatchItem.Groups[2].Value; if (lastSubIndex != hrefMatchItem.Index) { result.Append(emojiText.Substring(lastSubIndex, hrefMatchItem.Index - lastSubIndex)); } info.startIndex = result.Length; info.maxIndex = info.startIndex + info.text.Length - 1; mHrefInfoDic[info.startIndex] = info; result.Append(info.text); lastSubIndex = hrefMatchItem.Index + hrefMatchItem.Length; } if (hrefMatchItem == null) { text = emojiText; } else if (lastSubIndex != emojiText.Length - hrefMatchItem.Length) { result.Append(emojiText.Substring(lastSubIndex)); emojiText = result.ToString(); result = new StringBuilder(); } lastSubIndex = 0; } MatchCollection matches = Regex.Matches(emojiText, mEmojiAtlasInfo.pattern); Match matchItem = null; for (int i = 0; i < matches.Count; i++) { EmojiInfo info; if (mEmojiIndex.TryGetValue(matches[i].Value, out info)) { matchItem = matches[i]; if (mEmojiAtlasInfo.placeholder) { if (lastSubIndex != matchItem.Index) { result.Append(emojiText.Substring(lastSubIndex, matchItem.Index - lastSubIndex)); } mEmojiDic.Add(result.Length, info); result.Append(cPlaceholderChar); lastSubIndex = matchItem.Index + matchItem.Length; } else { mEmojiDic.Add(matchItem.Index, info); matchItem = null; } } } if (matchItem == null) { text = emojiText; return; } else if (lastSubIndex != emojiText.Length - matchItem.Length) { result.Append(emojiText.Substring(lastSubIndex)); } text = result.ToString(); }
/// <summary> /// Finally, the output text hyperlinks get parsed /// </summary> /// <returns></returns> protected string GetOutputText() { s_TextBuilder.Length = 0; var indexText = 0; fixedString = this.text; if (inspectorIconList != null && inspectorIconList.Length > 0) { foreach (IconName icon in inspectorIconList) { if (!string.IsNullOrEmpty(icon.name)) { fixedString = fixedString.Replace(icon.name, "<quad name=" + icon.name + " size=" + fontSize + " width=1 />"); } } } int count = 0; foreach (Match match in s_HrefRegex.Matches(fixedString)) { s_TextBuilder.Append(fixedString.Substring(indexText, match.Index - indexText)); s_TextBuilder.Append("<color=" + hyperlinkColor + ">"); // Hyperlink color var group = match.Groups[1]; if (isCreating_m_HrefInfos) { var hrefInfo = new HrefInfo { startIndex = s_TextBuilder.Length * 4, // Hyperlinks in text starting vertex indices endIndex = (s_TextBuilder.Length + match.Groups[2].Length - 1) * 4 + 3, name = group.Value }; m_HrefInfos.Add(hrefInfo); } else { if (m_HrefInfos.Count > 0) { m_HrefInfos[count].startIndex = s_TextBuilder.Length * 4; // Hyperlinks in text starting vertex indices; m_HrefInfos[count].endIndex = (s_TextBuilder.Length + match.Groups[2].Length - 1) * 4 + 3; count++; } } s_TextBuilder.Append(match.Groups[2].Value); s_TextBuilder.Append("</color>"); indexText = match.Index + match.Length; } // we should create array only once or if there is any change in the text if (isCreating_m_HrefInfos) { isCreating_m_HrefInfos = false; } s_TextBuilder.Append(fixedString.Substring(indexText, fixedString.Length - indexText)); m_OutputText = s_TextBuilder.ToString(); m_ImagesVertexIndex.Clear(); MatchCollection matches = s_Regex.Matches(m_OutputText); if (matches != null && matches.Count > 0) { foreach (Match match in matches) { var picIndex = match.Index; var endIndex = picIndex * 4 + 3; m_ImagesVertexIndex.Add(endIndex); } } return(m_OutputText); }
/// <summary> /// Finally, the output text hyperlinks get parsed /// </summary> /// <returns></returns> protected string GetOutputText() { s_TextBuilder.Length = 0; indexText = 0; fixedString = this.text; if (inspectorIconList != null && inspectorIconList.Length > 0) { for (int i = 0; i < inspectorIconList.Length; i++) { if (!string.IsNullOrEmpty(inspectorIconList[i].name)) { fixedString = fixedString.Replace(inspectorIconList[i].name, "<quad name=" + inspectorIconList[i].name + " size=" + fontSize + " width=1 />"); } } } count = 0; href_matches = s_HrefRegex.Matches(fixedString); if (href_matches != null && href_matches.Count > 0) { for (int i = 0; i < href_matches.Count; i++) { s_TextBuilder.Append(fixedString.Substring(indexText, href_matches[i].Index - indexText)); s_TextBuilder.Append("<color=" + hyperlinkColor + ">"); // Hyperlink color var group = href_matches[i].Groups[1]; if (isCreating_m_HrefInfos) { HrefInfo hrefInfo = new HrefInfo { // Hyperlinks in text starting index startIndex = (usesNewRendering ? s_TextBuilder.Length : s_TextBuilder.Length * 4), endIndex = (usesNewRendering ? (s_TextBuilder.Length + href_matches[i].Groups[2].Length - 1) : (s_TextBuilder.Length + href_matches[i].Groups[2].Length - 1) * 4 + 3), name = group.Value }; m_HrefInfos.Add(hrefInfo); } else { if (m_HrefInfos.Count > 0) { // Hyperlinks in text starting index m_HrefInfos[count].startIndex = (usesNewRendering ? s_TextBuilder.Length : s_TextBuilder.Length * 4); m_HrefInfos[count].endIndex = (usesNewRendering ? (s_TextBuilder.Length + href_matches[i].Groups[2].Length - 1) : (s_TextBuilder.Length + href_matches[i].Groups[2].Length - 1) * 4 + 3); count++; } } s_TextBuilder.Append(href_matches[i].Groups[2].Value); s_TextBuilder.Append("</color>"); indexText = href_matches[i].Index + href_matches[i].Length; } } // we should create array only once or if there is any change in the text if (isCreating_m_HrefInfos) { isCreating_m_HrefInfos = false; } s_TextBuilder.Append(fixedString.Substring(indexText, fixedString.Length - indexText)); m_OutputText = s_TextBuilder.ToString(); m_ImagesVertexIndex.Clear(); matches = s_Regex.Matches(m_OutputText); #if UNITY_2019_1_OR_NEWER href_matches = s_HrefRegex.Matches(m_OutputText); indexes.Clear(); for (int r = 0; r < matches.Count; r++) { indexes.Add(matches[r].Index); } #endif if (matches != null && matches.Count > 0) { for (int i = 0; i < matches.Count; i++) { picIndex = matches[i].Index; #if UNITY_2019_1_OR_NEWER if (usesNewRendering) { charactersRemoved = 0; removeCharacters = remove_Regex.Matches(m_OutputText); for (int r = 0; r < removeCharacters.Count; r++) { if (removeCharacters[r].Index < picIndex && !indexes.Contains(removeCharacters[r].Index)) { charactersRemoved += removeCharacters[r].Length; } } for (int r = 0; r < i; r++) { charactersRemoved += (matches[r].Length - 1); } picIndex -= charactersRemoved; } #endif vertIndex = picIndex * 4 + 3; m_ImagesVertexIndex.Add(vertIndex); } } #if UNITY_2019_1_OR_NEWER if (usesNewRendering) { if (m_HrefInfos != null && m_HrefInfos.Count > 0) { for (int i = 0; i < m_HrefInfos.Count; i++) { startCharactersRemoved = 0; endCharactersRemoved = 0; removeCharacters = remove_Regex.Matches(m_OutputText); for (int r = 0; r < removeCharacters.Count; r++) { if (removeCharacters[r].Index < m_HrefInfos[i].startIndex && !indexes.Contains(removeCharacters[r].Index)) { startCharactersRemoved += removeCharacters[r].Length; } else if (removeCharacters[r].Index < m_HrefInfos[i].startIndex && indexes.Contains(removeCharacters[r].Index)) { startCharactersRemoved += removeCharacters[r].Length - 1; } if (removeCharacters[r].Index < m_HrefInfos[i].endIndex && !indexes.Contains(removeCharacters[r].Index)) { endCharactersRemoved += removeCharacters[r].Length; } else if (removeCharacters[r].Index < m_HrefInfos[i].endIndex && indexes.Contains(removeCharacters[r].Index)) { endCharactersRemoved += removeCharacters[r].Length - 1; } } m_HrefInfos[i].startIndex -= startCharactersRemoved; m_HrefInfos[i].startIndex = m_HrefInfos[i].startIndex * 4; m_HrefInfos[i].endIndex -= endCharactersRemoved; m_HrefInfos[i].endIndex = m_HrefInfos[i].endIndex * 4 + 3; } } } #endif return(m_OutputText); }
protected override void OnPopulateMesh(VertexHelper toFill) { if (font == null) { return; } // We don't care if we the font Texture changes while we are doing our Update. // The end result of cachedTextGenerator will be valid for this instance. // Otherwise we can get issues like Case 619238. m_DisableFontTextureRebuiltCallback = true; Vector2 extents = rectTransform.rect.size; var settings = GetGenerationSettings(extents); cachedTextGenerator.Populate(text, settings); Rect inputRect = rectTransform.rect; // get the text alignment anchor point for the text in local space Vector2 textAnchorPivot = GetTextAnchorPivot(alignment); Vector2 refPoint = Vector2.zero; refPoint.x = Mathf.Lerp(inputRect.xMin, inputRect.xMax, textAnchorPivot.x); refPoint.y = Mathf.Lerp(inputRect.yMin, inputRect.yMax, textAnchorPivot.y); // Determine fraction of pixel to offset text mesh. Vector2 roundingOffset = PixelAdjustPoint(refPoint) - refPoint; // Apply the offset to the vertices IList <UIVertex> verts = cachedTextGenerator.verts; float unitsPerPixel = 1 / pixelsPerUnit; //Last 4 verts are always a new line... int vertCount = verts.Count - 4; toFill.Clear(); if (roundingOffset != Vector2.zero) { for (int i = 0; i < vertCount; ++i) { int tempVertsIndex = i & 3; m_TempVerts[tempVertsIndex] = verts[i]; m_TempVerts[tempVertsIndex].position *= unitsPerPixel; m_TempVerts[tempVertsIndex].position.x += roundingOffset.x; m_TempVerts[tempVertsIndex].position.y += roundingOffset.y; if (tempVertsIndex == 3) { toFill.AddUIVertexQuad(m_TempVerts); } } } else { EmojiInfo info; int hrefIndex = 0; HrefInfo hrefInfo = null; for (int i = 0; i < vertCount; ++i) { int tempVertsIndex = i & 3; m_TempVerts[tempVertsIndex] = verts[i]; m_TempVerts[tempVertsIndex].position *= unitsPerPixel; if (tempVertsIndex == 3) { int index = i / 4; if (hrefInfo != null || mHrefInfoDic.TryGetValue(hrefIndex, out hrefInfo)) { if (hrefInfo.startIndex == hrefIndex) { hrefInfo.rects.Clear(); } var bound = new Bounds(m_TempVerts[0].position, Vector3.zero); bound.Encapsulate(m_TempVerts[1].position); bound.Encapsulate(m_TempVerts[2].position); bound.Encapsulate(m_TempVerts[3].position); hrefInfo.rects.Add(new Rect(bound.min, bound.size)); if (hrefIndex >= hrefInfo.maxIndex) { hrefInfo = null; } } if (mEmojiDic.TryGetValue(index, out info)) { hrefIndex += 3; //float offset = 4; //m_TempVerts[0].position = m_TempVerts[0].position + new Vector3(-offset, offset); //m_TempVerts[1].position = m_TempVerts[1].position + new Vector3(offset, offset); //m_TempVerts[2].position = m_TempVerts[2].position + new Vector3(offset, -offset); //m_TempVerts[3].position = m_TempVerts[3].position + new Vector3(-offset, -offset); //m_TempVerts[0].uv0 = new Vector2(4, 4); //m_TempVerts[1].uv0 = new Vector2(4, 4); //m_TempVerts[2].uv0 = new Vector2(4, 4); //m_TempVerts[3].uv0 = new Vector2(4, 4); m_TempVerts[0].uv1 = new Vector2(info.x, info.y); m_TempVerts[1].uv1 = new Vector2(info.x + info.width, info.y); m_TempVerts[2].uv1 = new Vector2(info.x + info.width, info.y - info.height); m_TempVerts[3].uv1 = new Vector2(info.x, info.y - info.height); var offsetY = m_TempVerts[1].position.y + m_TempVerts[3].position.x - m_TempVerts[2].position.x; int scaleOffset = 2; int xOffset = 3; m_TempVerts[0].position += new Vector3(-scaleOffset + xOffset, scaleOffset); m_TempVerts[1].position += new Vector3(scaleOffset + xOffset, scaleOffset); m_TempVerts[2].position = new Vector3(m_TempVerts[2].position.x, offsetY) + new Vector3(scaleOffset + xOffset, -scaleOffset); m_TempVerts[3].position = new Vector3(m_TempVerts[3].position.x, offsetY) + new Vector3(-scaleOffset + xOffset, -scaleOffset); //Debug.LogError(string.Format("{0},{1},{2},{3}", m_TempVerts[0].position, m_TempVerts[1].position, m_TempVerts[2].position, m_TempVerts[3].position)); //m_TempVerts[0].uv1 = new Vector2(info.x, info.y + info.height); //m_TempVerts[1].uv1 = new Vector2(info.x + info.width, info.y + info.height); //m_TempVerts[2].uv1 = new Vector2(info.x + info.width, info.y); //m_TempVerts[3].uv1 = new Vector2(info.x, info.y); } toFill.AddUIVertexQuad(m_TempVerts); hrefIndex++; } } } m_DisableFontTextureRebuiltCallback = false; }
private string GetOutputText() { if (null == _SpriteInfo) { _SpriteInfo = new Dictionary <int, SpriteTagInfo>(); } StringBuilder _textBuilder = new StringBuilder(); int _textIndex = 0; var matches = _InputTagRegex.Matches(text); foreach (Match match in matches) { int _tempID = 0; if (!string.IsNullOrEmpty(match.Groups[1].Value) && !match.Groups[1].Value.Equals("-")) { _tempID = int.Parse(match.Groups[1].Value); } string _tempTag = match.Groups[2].Value; //更新超链接 if (_tempID < 0) { _textBuilder.Append(text.Substring(_textIndex, match.Index - _textIndex)); _textBuilder.Append("<color=blue>"); int _startIndex = _textBuilder.Length * 4; _textBuilder.Append("["); _textBuilder.Append(match.Groups[2].Value); _textBuilder.Append("]"); int _endIndex = _textBuilder.Length * 4 - 2; _textBuilder.Append("</color>"); var hrefInfo = new HrefInfo { id = Mathf.Abs(_tempID), startIndex = _startIndex, // 超链接里的文本起始顶点索引 endIndex = _endIndex, name = match.Groups[2].Value }; _ListHrefInfos.Add(hrefInfo); } else //更新表情 { if (!_IndexSpriteInfo.ContainsKey(_tempID) || !_IndexSpriteInfo[_tempID].ContainsKey(_tempTag)) { continue; } SpriteInfoGroup _tempGroup = _IndexSpriteInfo[_tempID][_tempTag]; _textBuilder.Append(text.Substring(_textIndex, match.Index - _textIndex)); int _tempIndex = _textBuilder.Length * 4;// 每个字符四个顶点,用顶点的起始来索引表情 // 使用 quad 标记来表示表情占位符 _textBuilder.Append(@"<quad size="); _textBuilder.Append(_tempGroup.size); _textBuilder.Append(" width="); _textBuilder.Append(_tempGroup.width); _textBuilder.Append(" />"); if (!_SpriteInfo.ContainsKey(_tempIndex)) { SpriteTagInfo _tempSpriteTag = new SpriteTagInfo { _ID = _tempID, _Tag = _tempTag, _Size = new Vector2(_tempGroup.size * _tempGroup.width, _tempGroup.size), _Pos = new Vector3[4], // 表情的四个顶点 _UV = _tempGroup.listSpriteInfo[0].uv // 默认显示表情第一帧 }; _SpriteInfo.Add(_tempIndex, _tempSpriteTag); } } _textIndex = match.Index + match.Length; } _textBuilder.Append(text.Substring(_textIndex, text.Length - _textIndex)); return(_textBuilder.ToString()); }
void ParseText(string mText) { if (emojiData == null || !Application.isPlaying) { outputText = mText; return; } builder.Length = 0; emojis.Clear(); hrefs.Clear(); MatchCollection matches = Regex.Matches(mText, regexTag); if (matches.Count > 0) { int textIndex = 0; for (int i = 0; i < matches.Count; i++) { var match = matches[i]; matchResult.Parse(match, fontSize); switch (matchResult.type) { case MatchType.Emoji: { if (emojiData.TryGetValue(matchResult.title, out SpriteInfo info)) { builder.Append(mText.Substring(textIndex, match.Index - textIndex)); int temIndex = builder.Length; builder.Append("<quad size="); builder.Append(matchResult.height); builder.Append(" width="); builder.Append((matchResult.width * 1.0f / matchResult.height).ToString("f2")); builder.Append(" />"); emojis.Add(temIndex, new EmojiInfo() { type = MatchType.Emoji, sprite = info, width = matchResult.width, height = matchResult.height }); if (matchResult.HasUrl) { var hrefInfo = new HrefInfo() { show = false, startIndex = temIndex * 4, endIndex = temIndex * 4 + 3, url = matchResult.url, color = matchResult.GetColor(color) }; hrefs.Add(hrefInfo); } textIndex = match.Index + match.Length; } break; } case MatchType.HyperLink: { builder.Append(mText.Substring(textIndex, match.Index - textIndex)); builder.Append("<color="); builder.Append(matchResult.GetHexColor(color)); builder.Append(">"); var href = new HrefInfo { show = true, startIndex = builder.Length * 4 }; builder.Append(matchResult.link); href.endIndex = builder.Length * 4 - 1; href.url = matchResult.url; href.color = matchResult.GetColor(color); hrefs.Add(href); builder.Append("</color>"); textIndex = match.Index + match.Length; break; } } } builder.Append(mText.Substring(textIndex, mText.Length - textIndex)); outputText = builder.ToString(); } else { outputText = mText; } }
private string GetOutputText() { _SpriteInfo = new Dictionary <int, SpriteTagInfo>(); StringBuilder _textBuilder = new StringBuilder(); int _textIndex = 0; foreach (Match match in _InputTagRegex.Matches(text)) { int _tempID = 0; if (!string.IsNullOrEmpty(match.Groups[1].Value)) { _tempID = int.Parse(match.Groups[1].Value); } string _tempTag = match.Groups[2].Value; //更新超链接 if (_tempID == -1) { _textBuilder.Append(text.Substring(_textIndex, match.Index - _textIndex)); _textBuilder.Append("<color=blue>"); int _startIndex = _textBuilder.Length * 4; _textBuilder.Append("[" + match.Groups[2].Value + "]"); int _endIndex = _textBuilder.Length * 4 - 2; _textBuilder.Append("</color>"); var hrefInfo = new HrefInfo { startIndex = _startIndex, // 超链接里的文本起始顶点索引 endIndex = _endIndex, name = match.Groups[2].Value }; _ListHrefInfos.Add(hrefInfo); } //更新表情 else { if (!_InlineManager._IndexSpriteInfo.ContainsKey(_tempID) || !_InlineManager._IndexSpriteInfo[_tempID].ContainsKey(_tempTag)) { continue; } SpriteInforGroup _tempGroup = _InlineManager._IndexSpriteInfo[_tempID][_tempTag]; _textBuilder.Append(text.Substring(_textIndex, match.Index - _textIndex)); int _tempIndex = _textBuilder.Length * 4; _textBuilder.Append(@"<quad size=" + _tempGroup.size + " width=" + _tempGroup.width + " />"); SpriteTagInfo _tempSpriteTag = new SpriteTagInfo { _ID = _tempID, _Tag = _tempTag, _Size = new Vector2(_tempGroup.size * _tempGroup.width, _tempGroup.size), _Pos = new Vector3[4], _UV = _tempGroup.listSpriteInfor[0].uv }; if (!_SpriteInfo.ContainsKey(_tempIndex)) { _SpriteInfo.Add(_tempIndex, _tempSpriteTag); } } _textIndex = match.Index + match.Length; } _textBuilder.Append(text.Substring(_textIndex, text.Length - _textIndex)); return(_textBuilder.ToString()); }
private string GetOutputText() { _spriteInfo = new Dictionary <int, SpriteTagInfo>(); StringBuilder textBuilder = new StringBuilder(); int textIndex = 0; foreach (Match match in _inputTagRegex.Matches(text)) { int tempId = 0; if (!string.IsNullOrEmpty(match.Groups[1].Value) && !match.Groups[1].Value.Equals("-")) { tempId = int.Parse(match.Groups[1].Value); } string tempTag = match.Groups[2].Value; //更新超链接 if (tempId < 0) { textBuilder.Append(text.Substring(textIndex, match.Index - textIndex)); textBuilder.Append("<color=blue>"); int startIndex = textBuilder.Length * 4; textBuilder.Append("[" + match.Groups[2].Value + "]"); int endIndex = textBuilder.Length * 4 - 2; textBuilder.Append("</color>"); var hrefInfo = new HrefInfo { Id = Mathf.Abs(tempId), StartIndex = startIndex, // 超链接里的文本起始顶点索引 EndIndex = endIndex, Name = match.Groups[2].Value }; _listHrefInfos.Add(hrefInfo); } //更新表情 else { if (!_inlineManager.IndexSpriteInfo.ContainsKey(tempId) || !_inlineManager.IndexSpriteInfo[tempId].ContainsKey(tempTag)) { continue; } SpriteInforGroup tempGroup = _inlineManager.IndexSpriteInfo[tempId][tempTag]; textBuilder.Append(text.Substring(textIndex, match.Index - textIndex)); int tempIndex = textBuilder.Length * 4; textBuilder.Append(@"<quad size=" + tempGroup.Size + " width=" + tempGroup.Width + " />"); SpriteTagInfo tempSpriteTag = new SpriteTagInfo { Id = tempId, Tag = tempTag, Size = new Vector2(tempGroup.Size * tempGroup.Width, tempGroup.Size), Pos = new Vector3[4], Uv = tempGroup.ListSpriteInfor[0].Uv }; if (!_spriteInfo.ContainsKey(tempIndex)) { _spriteInfo.Add(tempIndex, tempSpriteTag); } } textIndex = match.Index + match.Length; } textBuilder.Append(text.Substring(textIndex, text.Length - textIndex)); return(textBuilder.ToString()); }