[寒江孤叶丶的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 ;) 免责申明:本博客提供的所有翻译文章原稿均来自互联网,仅供学习交流 ...
随机推荐
- WPF中ListBox ListView数据翻页浏览笔记(强调:是数据翻页,非翻页动画)
ListBox和ListView在应用中,常常有需求关于每页显示固定数量的数据,然后通过Timer自动或者手动翻页操作,本文介绍到的就是该动作的实现. 一.重点 对于ListBox和ListView来 ...
- Gym-101158J Cover the Polygon with Your Disk 计算几何 求动圆与多边形最大面积交
题面 题意:给出小于10个点形成的凸多边形 和一个半径为r 可以移动的圆 求圆心在何处的面积交最大,面积为多少 题解:三分套三分求出圆心位置,再用圆与多边形面积求交 #include<bits/ ...
- JAVA 常用集合接口List、Set、Map总结
java中频繁使用List.Set.Map接口,将其总结如下 它们的继承与实现关系如下: Collection├List│├LinkedList│├ArrayList│└Vector│ └Stack└ ...
- java中字符串比较==和equals
1 总体来说java中字符串的比较是==比较引用,equals 比较值的做法.(equals 对于其他引用类型比较的是地址,这是因为object的equals方法比较的是引用),但是不同的声明方法字符 ...
- 有关PHP里的Cookie
会话技术:Cookie,Session: Cookie是客户端技术,服务器把每个用户的数据以Cookie的形式保存在用户各自的浏览器,当用户用浏览器再次访问服务器的web资源时则会带着各自的数据去了: ...
- vue2.x阅读笔记
v-once 作用: 只赋值一次 注意: 包括子项都是只赋值一次 模板 1.v-text v-html 2.{}语法 3.如果是html的属性,则用v-bind绑定,简写: 注意: 1.可以使用jav ...
- vue-cli 结构
. |-- build // 项目构建(webpack)相关代码 | |-- build.js // ...
- 从Spark1.6到Spark2.1,Logging该何去何从
大家都知道spark 1.6.0版本比较稳定,也比较流行. 我们项目组也是,最初用的就是这个版本. 这段时间,项目组引入spark 2.1.0版本,我想尝尝鲜. Pom中刚刚换了dependency马 ...
- [Intermediate Algorithm] - Spinal Tap Case
题目 将字符串转换为 spinal case.Spinal case 是 all-lowercase-words-joined-by-dashes 这种形式的,也就是以连字符连接所有小写单词. 提示 ...
- 02--读书笔记之:C++ Primer (第4版)及习题
推荐博客:http://www.cnblogs.com/xkfz007/archive/2012/08/15/2639381.html 第2章 数据和基本类型 1. 整型 2. 习题:左值和右值 3. ...