VGUI RichText

From Valve Developer Community
Jump to: navigation, search

RichText is a vgui2 element defined in the vgui_controls library, in the file RichText.cpp. RichText is available in all Source games. RichText is useful for displaying blocks or paragraphs of text, as opposed to VGUI Label, which is used to display small amounts of text.

The difference between RichText and Label lies in the functionality of RichText. RichText is more useful for displaying blocks of text because it has scrollbars, borders, text formatting, and text wrapping. Note that the scrollbars and borders can also be disabled if necessary.

This is a screenshot of some example RichText. Note that a common bug is visible.

Example Usage

Before creating anything, we first need to include the RichText header file and use the vgui namespace.

#include <vgui_controls/RichText.h>
using namespace vgui;


RichText can be created via the c++ keyword new with a simple call to RichText:

RichText* txt = new RichText(this, "myText");


Once we have our RichText created, we now need to configure it (usually done in ApplySchemeSettings):

void CItemMenu::ApplySchemeSettings( vgui::IScheme *pScheme )
{
	BaseClass::ApplySchemeSettings( pScheme );

       txt->SetText("This is an example of rich text. Rich text is useful for droning on and on about a subject...");
       txt->SetFont(pScheme->GetFont( "DefaultVerySmall" ));
       txt->SetPos(200,200);
       txt->SetSize(200,200);
       txt->SetVisible(true);
}


And we're done!

Known Problems with RichText

Sometimes the text will extend past the bottom of the RichText border. You can see an example of this in the screenshot. You can sometimes work around this by changing the control's height.