[寒江孤叶丶的Cocos2d-x之旅_33]RichTextEx一款通过HTML标签控制文字样式的富文本控件
RichTextEx一款通过HTML标签控制文字样式的富文本控件
原创文章,欢迎转载。转载请注明:文章来自[寒江孤叶丶的Cocos2d-x之旅系列]
博客地址:http://blog.csdn.net/qq446569365
下载地址
这个是干什么的
将例如以下文字内容
"<#F37C2A><font Helvetica><30>【世】<#3AB5B3><underLine true>寒江孤叶<underLine false><#F8F4D7>:HelloWorld"
生成如图所看到的样式的RichText(支持图片以及闪烁、旋转和其它自己定义的效果、控件)

关于它
这个是LUA版本号的。CPP版本号的没写,欢迎移植CPP和JS版本号
LUA文件是用一个别人写的文件改动的(增加一些功能。修复几个BUG……话说之前跑都跑不起来啊亲……什么鬼)
另外抱歉,找不到他的Github链接了……
· TTF字体支持描边。系统字体是不支持的
使用说明
RichTextEx使用起来很easy,仅仅要将RichTextEx.lua拷贝到你的项目文件夹中,并require它就能够了
比方这样:
APUtils = APUtils or {}
APUtils.RichTextEx = APUtils.RichTextEx or require("APUtils/gui/RichTextEx.lua")
使用RichText来创建一个富文本是很easy的:
local txt = RichTextEx:create() -- 或 RichTextEx:create(26, cc.c3b(10, 10, 10))
txt:setText("<outLine 5><underLine true><#EFB65C><font res/fonts/pw.ttf><24>您的元宝和银券不足请<#FF0000><35>充值<#EFB65C><24>,或领取抽取元宝奖励!")
-- 多行模式要同一时候设置 ignoreContentAdaptWithSize(false) 和 contentSize
txt:setMultiLineMode(true) -- 这行事实上就是 ignoreContentAdaptWithSize(false)
txt:setContentSize(200, 400)
someNode:addChild(txt)
假设字符串是由用户输入的话,建议调用RichTextEx.htmlEncode("<ABC>")将用户输入内容编码一下,以避免用户输入keyword符导致无法预知的错误
在生成字符串之前会自己主动调用RichTextEx.htmlDecode,假设你自己定义了用于显示文字内容的控件,请记得调用它,以对字符串进行解码
RichTextEx的基本选项
<#F00> = <#FF0000> = 文字颜色
<32> = 字体大小
<font Arial> = 文字字体 支持TTF
<img filename> = 图片(filename 能够是已经在 SpriteFrameCache 里的 key。或磁盘文件)
<img_32*32 fname> = 指定大小的图片
<+2> <-2> <*2> </2> = 当前字体大小 +-*/
<!> = 颜色、字体和字体大小恢复默认
\n \t = 换行 和 tab,可能临时实现得不是很好 最好不要用 假设须要换行你能够创建多个RichText然后依次放好
<outLine 1> = 设置1像素描边,仅仅支持TTF字体
<underLine true> = 是否开启下划线
RichTextEx的演示样例选项 (在 RichTextEx.defaultCb 中提供)
<blink 文字> = (动画)闪烁那些文字
<rotate 文字> = (动画)旋转那些文字
<scale 文字> = (动画)缩放那些文字
(但假设你做了 setText(t, callback) 除非你在 callback 主动调用 defaultCb。否则以上选项会被忽略)
你能够对功能进行扩展
`<img_w*h http://path/image> 比如从网络下载图片`
同一时候支持自己定义特殊语法,增加 callback 回调就可,如
txt:setText("XXXXX <aaaa haha> <bbbb> <CCCC> xxx", function(text, sender) -- 第二个參数 sender 可选
-- 对每个自己定义的 <***> 都会调用此 callback
-- text 就等于 *** (不含<>)
-- 简单的返回一个 Node 的子实例就可,如
-- 假设接收第二个參数 sender,就可获取当前文字大小、颜色: sender._fontSize、sender._textColor
if string.sub(text, 1, 4) == "aaaa" then
return ccui.Text:create("aaa111" .. string.sub(text, 6)), "", 32)
--这里假设为了代码的健壮性最好增加self:htmlDecode
--return ccui.Text:create(self:htmlDecode("aaa111" .. string.sub(text, 6))), "", 32)
elseif text == "bbbb" then
-- 用当前文字大小和颜色
local lbl = ccui.Text:create("bbb111", "", sender._fontSize)
lbl:setTextColor(sender._textColor)
return lbl
elseif string.sub(text, 1, 4) == "CCCC" then
local img = ccui.ImageView:create(....)
img:setScale(...)
img:runAction(...)
return img
end
end)
你还要做什么
为了支持描边和下划线还要改动一下Cocos的源代码
UIRichText.h和UIRichText.cpp放到项目源代码文件夹 替换原来的
路径:frameworks/cocos2d-x/cocos/ui
(改动内容主要三个方面:增加下划线设置,增加描边设置,RichText能够自己主动更改高度了)
另外还须要改动toLua文件
frameworks/cocos2d-x/cocos/scripting/lua-bindings/auto/lua_cocos2dx_ui_auto.cpp
18878行左右能看到两个函数
int lua_cocos2dx_ui_RichElementText_init(lua_State* tolua_S)
和
int lua_cocos2dx_ui_RichElementText_create(lua_State* tolua_S)
将这两个函数的实现替换为例如以下形式:
int lua_cocos2dx_ui_RichElementText_init(lua_State* tolua_S)
{
int argc = 0;
cocos2d::ui::RichElementText* cobj = nullptr;
bool ok = true;
#if COCOS2D_DEBUG >= 1
tolua_Error tolua_err;
#endif
#if COCOS2D_DEBUG >= 1
if (!tolua_isusertype(tolua_S,1,"ccui.RichElementText",0,&tolua_err)) goto tolua_lerror;
#endif
cobj = (cocos2d::ui::RichElementText*)tolua_tousertype(tolua_S,1,0);
#if COCOS2D_DEBUG >= 1
if (!cobj)
{
tolua_error(tolua_S,"invalid 'cobj' in function 'lua_cocos2dx_ui_RichElementText_init'", nullptr);
return 0;
}
#endif
argc = lua_gettop(tolua_S)-1;
if (argc == 8)
{
int arg0;
cocos2d::Color3B arg1;
uint16_t arg2;
std::string arg3;
std::string arg4;
double arg5;
int arg6;
bool arg7;
ok &= luaval_to_int32(tolua_S, 2,(int *)&arg0, "ccui.RichElementText:init");
ok &= luaval_to_color3b(tolua_S, 3, &arg1, "ccui.RichElementText:init");
ok &= luaval_to_uint16(tolua_S, 4,&arg2, "ccui.RichElementText:init");
ok &= luaval_to_std_string(tolua_S, 5,&arg3, "ccui.RichElementText:init");
ok &= luaval_to_std_string(tolua_S, 6,&arg4, "ccui.RichElementText:init");
ok &= luaval_to_number(tolua_S, 7,&arg5, "ccui.RichElementText:init");
ok &= luaval_to_int32(tolua_S, 8,&arg6, "ccui.RichElementText:init");
ok &= luaval_to_boolean(tolua_S, 9,&arg7, "ccui.RichElementText:init");
if(!ok)
{
tolua_error(tolua_S,"invalid arguments in function 'lua_cocos2dx_ui_RichElementText_init'", nullptr);
return 0;
}
bool ret = cobj->init(arg0, arg1, arg2, arg3, arg4, arg5,arg6,arg7);
tolua_pushboolean(tolua_S,(bool)ret);
return 1;
}
luaL_error(tolua_S, "%s has wrong number of arguments: %d, was expecting %d \n", "ccui.RichElementText:init",argc, 6);
return 0;
#if COCOS2D_DEBUG >= 1
tolua_lerror:
tolua_error(tolua_S,"#ferror in function 'lua_cocos2dx_ui_RichElementText_init'.",&tolua_err);
#endif
return 0;
}
int lua_cocos2dx_ui_RichElementText_create(lua_State* tolua_S)
{
int argc = 0;
bool ok = true;
#if COCOS2D_DEBUG >= 1
tolua_Error tolua_err;
#endif
#if COCOS2D_DEBUG >= 1
if (!tolua_isusertable(tolua_S,1,"ccui.RichElementText",0,&tolua_err)) goto tolua_lerror;
#endif
argc = lua_gettop(tolua_S) - 1;
if (argc == 6)
{
int arg0;
cocos2d::Color3B arg1;
uint16_t arg2;
std::string arg3;
std::string arg4;
double arg5;
ok &= luaval_to_int32(tolua_S, 2,(int *)&arg0, "ccui.RichElementText:create");
ok &= luaval_to_color3b(tolua_S, 3, &arg1, "ccui.RichElementText:create");
ok &= luaval_to_uint16(tolua_S, 4,&arg2, "ccui.RichElementText:create");
ok &= luaval_to_std_string(tolua_S, 5,&arg3, "ccui.RichElementText:create");
ok &= luaval_to_std_string(tolua_S, 6,&arg4, "ccui.RichElementText:create");
ok &= luaval_to_number(tolua_S, 7,&arg5, "ccui.RichElementText:create");
if(!ok)
{
tolua_error(tolua_S,"invalid arguments in function 'lua_cocos2dx_ui_RichElementText_create'", nullptr);
return 0;
}
cocos2d::ui::RichElementText* ret = cocos2d::ui::RichElementText::create(arg0, arg1, arg2, arg3, arg4, arg5);
object_to_luaval<cocos2d::ui::RichElementText>(tolua_S, "ccui.RichElementText",(cocos2d::ui::RichElementText*)ret);
return 1;
}
if (argc == 7)
{
int arg0;
cocos2d::Color3B arg1;
uint16_t arg2;
std::string arg3;
std::string arg4;
double arg5;
int arg6;
ok &= luaval_to_int32(tolua_S, 2,(int *)&arg0, "ccui.RichElementText:create");
ok &= luaval_to_color3b(tolua_S, 3, &arg1, "ccui.RichElementText:create");
ok &= luaval_to_uint16(tolua_S, 4,&arg2, "ccui.RichElementText:create");
ok &= luaval_to_std_string(tolua_S, 5,&arg3, "ccui.RichElementText:create");
ok &= luaval_to_std_string(tolua_S, 6,&arg4, "ccui.RichElementText:create");
ok &= luaval_to_number(tolua_S, 7,&arg5, "ccui.RichElementText:create");
ok &= luaval_to_int32(tolua_S, 8,&arg6, "ccui.RichElementText:create");
if(!ok)
{
tolua_error(tolua_S,"invalid arguments in function 'lua_cocos2dx_ui_RichElementText_create'", nullptr);
return 0;
}
cocos2d::ui::RichElementText* ret = cocos2d::ui::RichElementText::create(arg0, arg1, arg2, arg3, arg4, arg5, arg6);
object_to_luaval<cocos2d::ui::RichElementText>(tolua_S, "ccui.RichElementText",(cocos2d::ui::RichElementText*)ret);
return 1;
}
if (argc == 8)
{
int arg0;
cocos2d::Color3B arg1;
uint16_t arg2;
std::string arg3;
std::string arg4;
double arg5;
int arg6;
bool arg7;
ok &= luaval_to_int32(tolua_S, 2,(int *)&arg0, "ccui.RichElementText:create");
ok &= luaval_to_color3b(tolua_S, 3, &arg1, "ccui.RichElementText:create");
ok &= luaval_to_uint16(tolua_S, 4,&arg2, "ccui.RichElementText:create");
ok &= luaval_to_std_string(tolua_S, 5,&arg3, "ccui.RichElementText:create");
ok &= luaval_to_std_string(tolua_S, 6,&arg4, "ccui.RichElementText:create");
ok &= luaval_to_number(tolua_S, 7,&arg5, "ccui.RichElementText:create");
ok &= luaval_to_int32(tolua_S, 8,&arg6, "ccui.RichElementText:create");
ok &= luaval_to_boolean(tolua_S, 9,&arg7, "ccui.RichElementText:create");
if(!ok)
{
tolua_error(tolua_S,"invalid arguments in function 'lua_cocos2dx_ui_RichElementText_create'", nullptr);
return 0;
}
cocos2d::ui::RichElementText* ret = cocos2d::ui::RichElementText::create(arg0, arg1, arg2, arg3, arg4, arg5, arg6, arg7);
object_to_luaval<cocos2d::ui::RichElementText>(tolua_S, "ccui.RichElementText",(cocos2d::ui::RichElementText*)ret);
return 1;
}
luaL_error(tolua_S, "%s has wrong number of arguments: %d, was expecting %d\n ", "ccui.RichElementText:create",argc, 6);
return 0;
#if COCOS2D_DEBUG >= 1
tolua_lerror:
tolua_error(tolua_S,"#ferror in function 'lua_cocos2dx_ui_RichElementText_create'.",&tolua_err);
#endif
return 0;
}
又一次编译一下项目,然后就能够在项目里用了
下个版本号要更新的内容
1.继续改动Cocos2d-x的RichText的源代码。使其更好的支持tab和换行
2.增加可点击的文字,以及点击后变色
3.为系统字体增加描边(推断为系统字体时,描边採用阴影替代)
其它
下划线实现的很拙略,假设你有更好的方法一定要告诉我。
或许您希望得到一个不须要改动Cocos2d-x源代码的版本号(此版本号不支持描边和下划线)
欢迎交流QQ:446569365
[寒江孤叶丶的Cocos2d-x之旅_33]RichTextEx一款通过HTML标签控制文字样式的富文本控件的更多相关文章
- [寒江孤叶丶的Cocos2d-x之旅_32]微信输入框风格的IOS平台的EditBox
原创文章,欢迎转载.转载请注明:文章来自[寒江孤叶丶的Cocos2d-x之旅系列] 博客地址:http://blog.csdn.net/qq446569365 偶然看到一个游戏注冊账号时候,输入框是弹 ...
- [寒江孤叶丶的Cocos2d-x之旅_36]用LUA实现UTF8的字符串基本操作 UTF8字符串长度,UTF8字符串剪裁等
原创文章,欢迎转载,转载请注明:文章来自[寒江孤叶丶的Cocos2d-x之旅系列] 博客地址:http://blog.csdn.net/qq446569365 一个用于UTF8字符串操作的类.功能比較 ...
- ERROR: Symbol file could not be found 寒江孤钓<<windows 内核安全编程>> 学习笔记
手动下载了Symbols,设置好了Symbols File Path,串口连接上了以后,出现ERROR: Symbol file could not be found, 并且会一直不停的出现windb ...
- Debuggee not connected 寒江孤钓<<windows 内核安全编程>> 学习笔记
双机联调出现的问题 真机系统win7 虚拟机系统xp 安装书中的配置一步一步走,发现最后启动系统后,windbg一直显示 Opened \\.\pipe\com_1Waiting to reconne ...
- 指定的服务已标记为删除 寒江孤钓<<windows 内核安全编程>> 学习笔记
运行cmd:"sc delete first" 删除我们的服务之后, 再次创建这个服务的时候出现 "指定的服务已标记为删除"的错误, 原因是我们删除服务之前没有 ...
- 删除自定义服务 寒江孤钓<<windows 内核安全编程>> 学习笔记
书中第一章 成功启动first服务之后, 发现书中并没有介绍删除服务的方式, SRVINSTW工具中的移除服务功能,也无法找到我们要删除的服务, 于是,搜素了下,发现解决方法如下: 在cmd中输入 & ...
- 发生系统错误 1275.此驱动程序被阻止加载 寒江孤钓<<windows 内核安全编程>> 学习笔记
安装书中第一章成功安装first服务之后,在cmd窗口使用命令行 "net start first" 时, 出现 "发生系统错误 1275.此驱动程序被阻止加载" ...
- 无法编译出.sys文件 寒江孤钓<<windows 内核安全编程>> 学习笔记
系统环境:win7 编译环境:Windows Win7 IA-64 Checked Build Environment 按照书中所说的步骤,出现如下问题 后来直接使用光盘源码,编译成功,于是对照源文件 ...
- Cocos2D iOS之旅:如何写一个敲地鼠游戏(十一):完善游戏逻辑
大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请告诉我,如果觉得不错请多多支持点赞.谢谢! hopy ;) 免责申明:本博客提供的所有翻译文章原稿均来自互联网,仅供学习交流 ...
随机推荐
- IE浏览器缓存导致Ajax请求失败
在IE浏览器中通过Ajax请求后台的数据,如果Page请求是postback类型的,可能会导致Ajax请求失败的问题 我们都知道ajax能提高页面载入的速度主要的原因是通过ajax减少了重复数据的载入 ...
- [hihocoder][Offer收割]编程练习赛44
扫雷游戏 #pragma comment(linker, "/STACK:102400000,102400000") #include<stdio.h> #includ ...
- SqlServer数据库表导入SqlLite数据库表保持日期时间类型字段的格式
在写查询功能的过程中遇到一个这样的问题:按日期范围查询,sql语句是:where dt>=用户选择起始日期&&dt<=用户选择结束日期.数据库中的数据如图1,我选择的测试数 ...
- svn SSL 错误:Key usage violation in certificate has been detected
CentOS/RHEL yum 安装的 subversion 是 1.6.11 版本,连VisualSVN服务器时会有"Key usage violation"的错误 将subve ...
- 给 iOS 开发者的 RxSwift(一)
RxSwift 或许我们都听说过,但或许只知道 RxSwift 这个单词,长篇大论关于 RxSwift 的介绍往往使读者迷失在各种概念当中,却不知如何让它大展伸手.或许我们可以换一种姿势,一些应用场景 ...
- webstorm主题网址
http://www.phpstorm-themes.com/ http://www.riaway.com/theme.php
- 路飞学城Python-Day101
57-多表操作之一对多添加纪录 def add(request): # pub = Publish.objects.create(name='人民出版社', email='873245193@qq.c ...
- Vue2实例中的data属性三种写法与作用
<script src="https://unpkg.com/vue/dist/vue.js"></script> <div id="app ...
- 【JavaScript框架封装】实现一个类似于JQuery的内容框架的封装
// 内容框架 (function (xframe) { // 需要参与链式访问的(必须使用prototype的方式来给对象扩充方法) xframe.extend({ /** * .html()用为读 ...
- nyoj286-动物统计
动物统计 时间限制:1000 ms | 内存限制:65535 KB 难度:2 描述 在美丽大兴安岭原始森林中存在数量繁多的物种,在勘察员带来的各种动物资料中有未统计数量的原始动物的名单.科学家想判 ...