[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
Tags lua

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

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

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

GitHub links

What does this do?

will be such as the following text content
"<#F37C2A><font Helvetica><30>【世】<#3AB5B3><underLine true>寒江孤叶<underLine false><#F8F4D7>:HelloWorld"
Generate the RichText of the style you see ( supports pictures and flashes, spins, and other effects, controls that you define yourself )

About it

This is the LUA version number. CPP version number is not written, welcome to migrate CPP and JS version number
Lua files are modified with a file written by someone else (adding some functionality.) 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 easy to use, just copy the Richtextex.lua to your project folder and require it to the
For example, this:

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

Using RichText to create a rich text is 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) 和 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 the keyword character .
call Richtextex.htmldecode yourself before generating the string, assuming you have defined the control you used 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>    = 是否开启下划线
Demo sample options for Richtextex (available in RICHTEXTEX.DEFAULTCB)
<blink 文字>      = (动画)闪烁那些文字<rotate 文字>     = (动画)旋转那些文字<scale 文字>      = (动画)缩放那些文字(但假设你做了 setText(t, callback) 除非你在 callback 主动调用 defaultCb。否则以上选项会被忽略)   
You can extend the functionality
` 比如从网络下载图片`

At the same time support yourself to define special syntax, add callback callback can, 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, change the source code for Cocos
UIRichText.h and UIRichText.cpp to replace the original
path in the project source folder: Frameworks/cocos2d-x/cocos/ui
(Change the main three aspects: Add underline settings, increase the stroke settings, RichText can change the height of their own initiative)
also need to change 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)
replaces the implementation of these two functions with the following form, for example:

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;}

Compile the project again and then you'll be able to use it in the project.

What to update for the next version number

1. Continue to change the source code of the Cocos2d-x RichText. Make it easier to support tab and line breaks
2. Add clickable text and change color after clicking
3. Add strokes to system fonts (when inferred as system fonts, strokes are replaced with shadows)

Other

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

[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.