[UGUI]图文混排(一):标签制定和解析

时间:2023-03-09 08:02:58
[UGUI]图文混排(一):标签制定和解析

参考链接:

https://github.com/SylarLi/RichText/tree/master/Assets/Scripts

正则表达式:

https://blog.csdn.net/lyh916/article/details/49201195

图文混排主要用于聊天,其实就是传输某种格式的字符串,然后解析这个字符串,生成表情文字等。图文混排的第一步,就是确定好格式,这里使用html的标签格式,对于代码中出现的start和end字段可以先忽略。标签格式如下:

<material=underline c=#ffffff h=1 n=*** p=***>blablabla...</material>

RichTextTag.cs

 //标签类型
public enum RichTextTagType
{
None,
Underline, //下划线
} //标签基类
public abstract class RichTextTag
{
public int start;
public int end;
public abstract RichTextTagType tagType { get; } public abstract void SetValue(string key, string value);
}

RichTextUnderlineTag.cs

 using UnityEngine;

 //下划线标签
public class RichTextUnderlineTag : RichTextTag { public Color color = Color.white;//颜色
public float height = 1f;//高度
public string eventName;//事件名
public string eventParameter;//事件参数 public override RichTextTagType tagType { get { return RichTextTagType.Underline; } } public override void SetValue(string key, string value)
{
switch (key)
{
case "c":
{
ColorUtility.TryParseHtmlString(value, out color);
break;
}
case "h":
{
float.TryParse(value, out height);
break;
}
case "n":
{
eventName = value;
break;
}
case "p":
{
eventParameter = value;
break;
}
default:
break;
}
}
}

RichTextTagParser.cs

 using System.Text.RegularExpressions;

 //解析一条标签:<material xxx>
public class RichTextTagParser { private static readonly Regex tagRegex = new Regex(@"<material=([^>\s]+)([^>]*)>");//(标签类型)(标签参数)
private static readonly Regex paraRegex = new Regex(@"(\w+)=([^\s]+)");//(key)=(value)
public string content;//<material=xxx xxx>
public int start;
public int end; public RichTextTag Parse()
{
RichTextTag tag = null;
Match match = tagRegex.Match(content);
if (match.Success)
{
string tagName = match.Groups[].Value;//标签类型
if (!tagName.StartsWith("#"))
{
var keyValueCollection = paraRegex.Matches(match.Groups[].Value);//标签参数
switch (tagName)
{
case "underline":
{
tag = new RichTextUnderlineTag();
break;
}
default:
break;
}
if (tag != null)
{
tag.start = start;
tag.end = end;
for (int i = ; i < keyValueCollection.Count; i++)
{
string key = keyValueCollection[i].Groups[].Value;
string value = keyValueCollection[i].Groups[].Value;
tag.SetValue(key, value);
}
}
}
}
return tag;
}
}

RichTextParser.cs

 using System.Collections.Generic;
using System.Text.RegularExpressions; //解析全部标签
public class RichTextParser { private static readonly Regex regex = new Regex(@"</*material[^>]*>");//<material xxx> or </material>
private const string endStr = "</material>"; private Stack<RichTextTagParser> tagParserStack;
private List<RichTextTag> tagList; public RichTextParser()
{
tagParserStack = new Stack<RichTextTagParser>();
tagList = new List<RichTextTag>();
} public void Parse(string richText, out List<RichTextTag> tags)
{
tagParserStack.Clear();
tagList.Clear();
Match match = regex.Match(richText);
while (match.Success)
{
if (match.Value == endStr)
{
if (tagParserStack.Count > )
{
RichTextTagParser tagParser = tagParserStack.Pop();
tagParser.end = match.Index - ;
if (tagParser.end >= tagParser.start)
{
RichTextTag tag = tagParser.Parse();
if (tag != null)
{
tagList.Add(tag);
}
}
}
}
else
{
RichTextTagParser tagParser = new RichTextTagParser();
tagParser.content = match.Value;
tagParser.start = match.Index + match.Length;
tagParserStack.Push(tagParser);
}
match = match.NextMatch();
}
tags = tagList;
}
}

测试如下:

 using UnityEngine;
using System.Collections.Generic; //下划线<material=underline c=#ffffff h=1 n=*** p=***>blablabla...</material>
public class RichTextTest : MonoBehaviour { private string a = @"123<material=underline c=#ff0000 h=1 n=name1 p=para1>blablabla...</material>qwe" +
@"<material=underline c=#00ff00 h=2 n=name2 p=para2>blablabla...</material>asd";
private List<RichTextTag> tagList; private void Start()
{
RichTextParser parser = new RichTextParser();
parser.Parse(a, out tagList);
for (int i = ; i < tagList.Count; i++)
{
RichTextUnderlineTag tag = tagList[i] as RichTextUnderlineTag;
Debug.Log(tag.color);
Debug.Log(tag.height);
Debug.Log(tag.eventName);
Debug.Log(tag.eventParameter);
}
}
}

结果:

[UGUI]图文混排(一):标签制定和解析