NGUI UILabel 富文本格式说明

4 篇文章 0 订阅

 

文本[ff0000]带[-][00ff00]颜[-][0000ff]色[-].  

这里的颜色是和默认颜色混色的结果,如果默认颜色为黑色,则相当于着色无效。

[b]粗体[/b]
[i]斜体[/i]
[u]下划线[/u]
[s]删除线[/s]
[b][u]大[i][s]杂[/u][/b]烩[/i][/s]

正常文本[sup]上标[/sup]
正常文本[sub]下标[/sub]
[ffff00]不支持[sup]上标[sub]下标[/sub][/sup]的嵌套[-]

[00ff00]支持[-][u][url=http://miaodadao.lofter.com]URL点击[/url][/u]


官方文档:

UILabel简介:http://www.tasharen.com/?page_id=166

UILabel文档:http://www.tasharen.com/forum/index.php?topic=6706


BBCode:

维基百科:BBCode

UILabel BBCode介绍


自定义表情:


采用BMFont时,可通过AddSymbol方式添加静态表情图标。

参考:http://www.cnblogs.com/martianzone/p/3593892.html


Unity本身也是原生支持Rich Text的。

好的,以下是一个简单的示例代码,可以在 Unity 和 NGUI 中实现显示带多个网络图片的富文本: ```csharp using UnityEngine; using System.Collections; using System.Collections.Generic; using System.Text.RegularExpressions; using UnityEngine.UI; public class RichText : MonoBehaviour { public UILabel label; public UIGrid imagesGrid; public GameObject imagePrefab; private List<string> urls = new List<string>(); // 正则表达式匹配网络图片的链接 private static readonly Regex UrlRegex = new Regex(@"\b(?:https?://|www\.)\S+\b", RegexOptions.Compiled | RegexOptions.IgnoreCase); public void SetText(string text) { // 清空之前的图片 foreach (Transform child in imagesGrid.transform) { Destroy(child.gameObject); } // 获取所有网络图片的链接 urls.Clear(); MatchCollection matches = UrlRegex.Matches(text); foreach (Match match in matches) { string url = match.Value; if (url.EndsWith(".jpg") || url.EndsWith(".png")) { urls.Add(url); } } // 将所有网络图片添加到 imagesGrid 中 foreach (string url in urls) { GameObject imageObject = Instantiate(imagePrefab, imagesGrid.transform); StartCoroutine(LoadImage(url, imageObject.GetComponent<UITexture>())); } // 将文本内容设置到 labellabel.text = text; } private IEnumerator LoadImage(string url, UITexture texture) { WWW www = new WWW(url); yield return www; if (www.error == null) { texture.mainTexture = www.texture; } else { Debug.LogError("Failed to load image: " + www.error); } } } ``` 使用方法: 1. 在 Unity 中创建一个 UI Root,添加一个 NGUILabel 和 Grid。 2. 将 RichText 脚本挂载到 Label 上,将 imagesGrid 和 imagePrefab 拖拽到脚本对应的字段中。 3. 调用 `SetText()` 方法设置富文本内容即可,例如: ```csharp RichText richText = GetComponent<RichText>(); richText.SetText("这是一段带图片的富文本,<img src=\"http://example.com/image1.jpg\"> 和 <img src=\"http://example.com/image2.png\">"); ``` 这样,就可以在 NGUI 中实现显示带多个网络图片的富文本了。注意,这里使用了 HTML 标记 `<img>` 来指定图片的链接,这是一种常见的富文本格式。同时,也可以根据需要修改正则表达式来支持不同的富文本格式
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值