Rich Text

The difference between rich text and ordinary text is:

Click in the main toolbarButton to generate a rich text.

Instance properties


Rich text supports dynamic creation, for example:

GRichTextField aRichTextField = new GRichTextField (); aRichTextField.SetSize (100,100); aRichTextField.text = &quot; <a href='xxx'>Hello World</a> &quot;;

The method to listen for link clicks in rich text is (this event is bubbling, that is, you can not listen on rich text, you can listen on its parent or grandfather components):

// Unity / Cry / MonoGame, the data in the EventContext is the href value.
    aRichTextField.onClickLink.Add (onClickLink);
    // AS3 / Egret, TextEvent.text is the href value.
    aRichTextField.addEventListener (TextEvent.LINK, onClickLink);
    // Egret, TextEvent.text is the href value.
    aRichTextField.addEventListener (TextEvent.LINK, this.onClickLink, this);
    // Laya, the parameter of onClickLink is the href value.
    aRichTextField.on (, this, this.onClickLink);
    // Cocos2dx, EventContext.getDataValue ().asString () is the value of href.
    aRichTextField-> addEventListener (UIEventType :: ClickLink, CC_CALLBACK_1 (AClass :: onClickLink, this));
    // CocosCreator, the first parameter of onClickLink is the href value, and the optional second parameter is fgui.Event
    aRichTextField.on (fgui.Event.LINK, this.onClickLink, this);

The most important feature of rich text is support for HTML parsing and rendering. Plain text style tags, such asFONTBI, These are generally well supported. Some other object tags, such asAIMG and others support different strengths in each engine:

HTML syntax

Unity / MonoGame version has more complete support for HTML parsing.

If you want to access objects in HTML, you can use the following methods:

// Number of HTML elements in the current text
    int cnt = aRichTextField.richTextField.htmlElementCount;
    // Get the HTML element with the specified index, 0 = <index <cnt
    HtmlElement element = aRichTextField.richTextField.GetHtmlElementAt (index);
    // Get the HTML element with the specified name. The name is specified by the name attribute in the HTML element.
    element = aRichTextField.richTextField.GetHtmlElement (elementName);
    // Get the HTML object corresponding to the HTML element. HTML对象的类型有HtmlImage、HtmlLink、HtmlInput等。
IHtmlObject htmlObject = (IHtmlObject)element.htmlObject;
HtmlImage image = (HtmlImage)htmlObject;

As another example, if you want to make the effect of moving the mouse over a link to display information:

int cnt = richText.htmlObjectCount;
    for (int i = 0; i <cnt; i ++)
        IHtmlObject obj = richText.GetHtmlObjectAt (i);
        if (obj is HtmlLink)
            ((HtmlLink) obj) .shape.onRollOver.Add (onLinkRollOver);
            ((HtmlLink) obj) .shape.onRollOut.Add (onLinkRollOut);
    // You can call GRoot.inst.ShowPopup, GRoot.inst.ShowTooltips or other processing in the processing of RollOver and RollOut.

You can also extend and implement your own IHtmlObject. You need to implement an IHtmlPageContext interface yourself and assign it to the htmlPageContext property of the RichTextField. Read the source code for details.