参考链接:

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]图文混排(一):标签制定和解析的更多相关文章

  1. Unity UGUI图文混排源码(三) -- 动态表情

    这里是根据图文混排源码(二)进一步修改的,其他链接也不贴了,就贴一个链接就好了,第一次看这文章的同学可以先去看看其他几篇文章 Unity UGUI图文混排源码(二):http://blog.csdn. ...

  2. Unity UGUI图文混排源码(二)

    Unity UGUI图文混排源码(一):http://blog.csdn.net/qq992817263/article/details/51112304 Unity UGUI图文混排源码(二):ht ...

  3. Unity UGUI图文混排源码(一)

    Unity UGUI图文混排源码(一):http://blog.csdn.net/qq992817263/article/details/51112304 Unity UGUI图文混排源码(二):ht ...

  4. Unity琐碎(3) UGUI 图文混排解决方案和优化

    感觉使用Unity之后总能看到各种各样解决混排的方案,只能说明Unity不够体恤下情啊.这篇文章主要讲一下个人在使用过程中方案选择和优化过程,已做记录.顺便提下,开源很多意味着坑,还是要开实际需求. ...

  5. [UGUI]图文混排(二):Text源码分析

    UGUI源码: https://bitbucket.org/Unity-Technologies/ui/downloads/?tab=tags 首先下载一份UGUI源码,这里我下载的版本是5.3.2f ...

  6. Unity UGUI图文混排(七) -- 下划线

    之前更新超链接的时候,忘了搭配实现一个下划线的功能,这篇文章就是来补上这一个功能,时间有点长,一方面没有很好的思路,一方面也没多少时间. 先在网上收集了一下下划线的实现操作,一种是在文本下再创建一个文 ...

  7. Unity UGUI图文混排(六) -- 超链接

    图文混排更新到超链接这儿,好像也差不多了,不过就在最后一点,博主也表现得相当不专业,直接整合了山中双木林同学提供的超链接的解决方案,博主甚至没来得及细看就直接复制了,但感觉还是挺好用的. 博主已经将超 ...

  8. [UGUI]图文混排(三):资源管理

    1.图文混排中的资源,主要是图片. 2.所谓的资源管理,可以分为资源对象池和资源加载这两部分.这里是为图文混排单独做一套资源管理,当然也可以改为调用项目中的资源管理. RichTextResource ...

  9. Unity UGUI图文混排(五) -- 一张图集对应多个Text

    继上一篇说的更新了一张图集对应多个Text的功能,为了节省资源嘛 这里,但是也没有舍弃之前的一个Text一个图集,因为我感觉应该两个都有用,于是我重新写了一个脚本 1.其实大体跟前面的都没变,解析标签 ...

随机推荐

  1. 集群RedHat6.5+JDK1.8+Hadoop2.7.3+Spark2.1.1+zookeeper3.4.6+kafka2.11+flume1.6环境搭建步骤

    1.RHEL 6.5系统安装配置图解教程(rhel-server-6.5) 2.在Linux下安装JDK图文解析 3.RedHat6.5上安装Hadoop集群 4.RedHat6.5安装Spark集群 ...

  2. 【java】匿名对象

    匿名对象使用的场景:1.如果一个对象只调用一个方法一次的时候,就可以用匿名对象来调用. 一般不会用匿名对象给属性赋值,无法获取属性值,每次new 都是一个新的对象. new Car().run();/ ...

  3. python show slave status

    #!/usr/bin/env python import MySQLdbimport contextlib @contextlib.contextmanagerdef mysql(Host,Port, ...

  4. 字节数组与String类型的转换

    还是本着上篇文章的原则,只不过在Delphi中string有点特殊! 先了解一下Delphi中的string 1. string = AnsiString = 长字符串,理论上长度不受限制,但其实受限 ...

  5. 如何在Visual Studio 2013中连接中国版的Azure

    http://diaosbook.com/Post/2014/8/23/connect-azure-cn-in-vs2013   VS2013的Server Explorer在第一次连接Azure的时 ...

  6. java.lang.OutOfMemoryError: Java heap space解决方法 (有问题咨询加微信)

    支付宝扫码领取最高99元红包,到店支付15天,双十二瓜分15亿,打开支付宝首页搜“555176706”领红包,领到大红包的小伙伴赶紧使用哦! //首先检查程序有没有限入死循环 这个问题主要还是由这个问 ...

  7. materializecss的水波纹效果

    参考http://www.materializecss.cn/waves.html <html lang="en"> <head> <meta cha ...

  8. Array and Colon in Matlab

    1. Colon x=1:4 % x=[1 2 3 4] x=1:2:5 % x=[1 3 5]  (递增值为2) 2. Array 用一个矩阵作为例子: A=[1 2 3; 4 5 6; 7 8 9 ...

  9. Python单元测试框架之pytest 4 -- 断言

    From: https://www.cnblogs.com/fnng/p/4774676.html Python单元测试框架之pytest -- 断言 2015-08-31 23:57 by 虫师, ...

  10. C/C++基础---算法概览

    符号概念 beg和end表示元素范围的迭代器 beg2表示第二个序列开始位置迭代器,end2表示第二个序列末尾迭代器(如果有).如没有则假定系列2至少与beg end表示的范围一样大.beg和beg2 ...