[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]图文混排(一):标签制定和解析的更多相关文章
- Unity UGUI图文混排源码(三) -- 动态表情
这里是根据图文混排源码(二)进一步修改的,其他链接也不贴了,就贴一个链接就好了,第一次看这文章的同学可以先去看看其他几篇文章 Unity UGUI图文混排源码(二):http://blog.csdn. ...
- Unity UGUI图文混排源码(二)
Unity UGUI图文混排源码(一):http://blog.csdn.net/qq992817263/article/details/51112304 Unity UGUI图文混排源码(二):ht ...
- Unity UGUI图文混排源码(一)
Unity UGUI图文混排源码(一):http://blog.csdn.net/qq992817263/article/details/51112304 Unity UGUI图文混排源码(二):ht ...
- Unity琐碎(3) UGUI 图文混排解决方案和优化
感觉使用Unity之后总能看到各种各样解决混排的方案,只能说明Unity不够体恤下情啊.这篇文章主要讲一下个人在使用过程中方案选择和优化过程,已做记录.顺便提下,开源很多意味着坑,还是要开实际需求. ...
- [UGUI]图文混排(二):Text源码分析
UGUI源码: https://bitbucket.org/Unity-Technologies/ui/downloads/?tab=tags 首先下载一份UGUI源码,这里我下载的版本是5.3.2f ...
- Unity UGUI图文混排(七) -- 下划线
之前更新超链接的时候,忘了搭配实现一个下划线的功能,这篇文章就是来补上这一个功能,时间有点长,一方面没有很好的思路,一方面也没多少时间. 先在网上收集了一下下划线的实现操作,一种是在文本下再创建一个文 ...
- Unity UGUI图文混排(六) -- 超链接
图文混排更新到超链接这儿,好像也差不多了,不过就在最后一点,博主也表现得相当不专业,直接整合了山中双木林同学提供的超链接的解决方案,博主甚至没来得及细看就直接复制了,但感觉还是挺好用的. 博主已经将超 ...
- [UGUI]图文混排(三):资源管理
1.图文混排中的资源,主要是图片. 2.所谓的资源管理,可以分为资源对象池和资源加载这两部分.这里是为图文混排单独做一套资源管理,当然也可以改为调用项目中的资源管理. RichTextResource ...
- Unity UGUI图文混排(五) -- 一张图集对应多个Text
继上一篇说的更新了一张图集对应多个Text的功能,为了节省资源嘛 这里,但是也没有舍弃之前的一个Text一个图集,因为我感觉应该两个都有用,于是我重新写了一个脚本 1.其实大体跟前面的都没变,解析标签 ...
随机推荐
- SSH框架搭建demo
1.新建Java Web工程 2.添加Struts2.1框架支持 去除冲突包:antlr-2.7.2.jar 一般项目加上这三个包足够,后期可以视项目需求增加支持包: 2.1配置web.xml文件 增 ...
- RC4被JDK8默认禁用导致腾讯QQ邮箱无法访问
7月29日开始,腾讯修改了邮箱的加密方式,导致我们线上的所有的腾讯代收.代发邮件的功能全部失效.解决方法在最后,如果需要可直接跳转至解决方法一节 问题出现 7月29日开始,线上的所有的腾讯代收.代发邮 ...
- 解决新版本Vivado打开老工程IP锁住的问题
解决新版本Vivado打开老工程IP锁住的问题 1.生成IP核的状态报告 Tools -> Report -> Report IP Status 2.点击Upgrade Selected ...
- overflow标签
有时父标签设置了固定的宽高,但子标签把父标签给撑开了,就要在父标签里加一个overflow标签,等于hidden超出的地方隐藏,等于auto超出的地方隐藏,并且多个滚动条 <div style= ...
- qt编程
http://www.zhihu.com/question/20054048 http://www.cnblogs.com/luoshupeng/archive/2011/05/01/2033743. ...
- VS中的类模板
在目录C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\Common7\IDE\ItemTemplates\CSharp\Co ...
- java中经常使用的Swing组件总结
1.按钮(Jbutton) Swing中的按钮是Jbutton,它是javax.swing.AbstracButton类的子类,swing中的按钮可以显示图像,并且可以将按钮设置为窗口的默认图标,而且 ...
- 黄聪:如何高效率存储微信中的 access_token
众所周知,在微信开发中,获取access_token 的接口每天的调用次数是有限制的,2000次应该是. 不过其实这些完全够用了,除非你不小心写了个循环,在1秒中内用完了. 每个access_toke ...
- Java第07次实验提纲(异常)
PTA与参考资料 题集:集合 异常实验文件 第1次实验 1.1 7-1 常用异常 如何进行强制转换.父类型转化为子类型常见错误. 如何捕获多种类型的异常 简要输出异常信息,System.out.pri ...
- PHP 中如何创建和修改数组?
PHP中使用array来创建一个数组:array( key=>value , key=>value …… )用方括号的语法来修改数组:$arr[] = value 例如:$arr = ar ...