[The Cocos2d-x journey of the lone Ye Shi in the Cold River _33] Richtextex a rich text control that controls text styles via HTML tags

Source: Internet
Author: User

Richtextex a rich text control that controls text styles via HTML tags

Original article, welcome reprint, Reprint please specify: article from [Cold River solitary Ye Shi cocos2d-x Tour Series]

Blog Address: http://blog.csdn.net/qq446569365

GitHub links

What does this do?

The following text content
"<#F37C2A><font Helvetica><30>【世】<#3AB5B3><underLine true>寒江孤叶<underLine false><#F8F4D7>:HelloWorld"
RichText of generated styles ( supports picture and flicker, rotation, and other custom effects, controls )

About it

This is the LUA version, the CPP version is not written, welcome to migrate CPP and JS version
Lua files are modified with a file written by someone else (add some features to fix a few bugs ...). I don't know how to run. What the hell?)
Also sorry, can't find his github link ...
· TTF fonts support strokes, system fonts are not supported

Instructions for use

Richtextex is very simple to use, just copy the Richtextex.lua to your project directory and require it.
such as this:

APUtils = APUtils or {}  APUtils.RichTextEx = APUtils.RichTextEx or require("APUtils/gui/RichTextEx.lua")

Using RichText to create a rich text is very simple:

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) 和 contentSizetxt:setMultiLineMode(true)  -- 这行其实就是 ignoreContentAdaptWithSize(false)txt:setContentSize(200, 400)someNode:addChild(txt)

If the string is entered by the user, it is recommended to RichTextEx.htmlEncode("<ABC>") encode the user input to avoid the unpredictable error caused by the user entering key characters
Richtextex.htmldecode is called automatically before the string is generated, and if you customize the control to display the text content, remember to call it to decode the string

Basic options for Richtextex
<#F00> = <#FF0000>  = 文字颜色<32>                = 字体大小<font Arial>        = 文字字体 支持TTF      = 图片(filename 可以是已经在 SpriteFrameCache 里的 key,或磁盘文件)   = 指定大小的图片<+2> <-2> <*2> </2> = 当前字体大小 +-*/<!>                 = 颜色、字体和字体大小恢复默认\n \t               = 换行 和 tab,可能暂时实现得不是很好 最好不要用 如果需要换行你可以创建多个RichText然后依次放好<outLine 1>         = 设置1像素描边,只支持TTF字体<underLine true>    = 是否开启下划线
Example options for Richtextex (available in RICHTEXTEX.DEFAULTCB)
<blink 文字>      = (动画)闪烁那些文字<rotate 文字>     = (动画)旋转那些文字<scale 文字>      = (动画)缩放那些文字(但如果你做了 setText(t, callback) 除非你在 callback 主动调用 defaultCb,否则以上选项会被忽略)   
You can extend the functionality
` 例如从网络下载图片`

Also support custom special syntax, add callback callback can be, such as

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    endend)
What else do you have to do?

to support strokes and underscores also modify the source code for Cocos
UIRichText.h and UIRichText.cpp to the project source directory to replace the original
path: Frameworks/cocos2d-x/cocos/ui
(Modify the main three aspects: Add underline settings, add stroke settings, RichText can automatically change the height)
also need to modify the Tolua file
Frameworks /cocos2d-x/cocos/scripting/lua-bindings/auto/lua_cocos2dx_ui_auto.cpp
18878 rows or so can see two functions
int lua_cocos2dx_ui _richelementtext_init (lua_state* tolua_s)
and
int lua_cocos2dx_ui_richelementtext_create (lua_State* Tolua_S) The
replaces the implementation of these two functions with the following form:

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_isusertyp E (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_co        Cos2dx_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 '", Nu            LLPTR);        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 ret Urn 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_isusertab Le (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,&AMP;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_richeleme   Nttext_create '. ", &tolua_err); #endif return 0;}

Recompile the project and then you can use it in the project.

What to update in the next release

1. Continue to modify the source code of Cocos2d-x RichText, so that it can better support tab and line wrapping
2. Add clickable text and change color after clicking
3. Add strokes to system fonts (when judged as system fonts, strokes are replaced with shadows)

Other

Underline the implementation of the very humble slightly, if you have a better way to be sure to tell me.
Perhaps you would like to get a version that does not need to modify the Cocos2d-x source code (this version does not support strokes and underscores)
Welcome to AC qq:446569365

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

[The Cocos2d-x journey of the lone Ye Shi in the Cold River _33] Richtextex a rich text control that controls text styles via HTML tags

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.