NOTE
The Text Editing framework provides interfaces for modifying the character and style data within an instance of TStandardText in response to user input. This example demonstrates the lowest-level mechanisms for creating and modifying text programmatically.
Creating styled text
You apply style data to characters by first constructing style objects with the values you want and applying these objects to the character range with the TText::AddStyles function. You can apply the style objects individually or collect them in a style set and apply the entire style set to the character range.
To apply a single style object to text, you:
To apply a set of styles to text simultaneously, you:
When you instantiate TStandardText, the system allocates just enough space to store the text being inserted at the time of construction. You can also request that additional space be allocated, so that you can add text without needing to reallocate space for the object. The system allocates storage for styles only if styles are used.
// Create an instance of text.
TStandardText text("Hello world!");
// Create a style set, add styles, and apply it to the text.
TStyleSet textStyleSet;
TToken chicagoFontName("Chicago");
textStyleSet.Add( TFontFamilyStyle(chicagoFontName) );
textStyleSet.Add( TFontPointSizeStyle::GetDefaultFontPointSizeStyle() );
textStyleSet.Add( TTextColorStyle::GetRed() );
text.AddStyles(textStyleSet, TTextRange::GetMaximumRange());
// Text is currently red, 12 point (or the current default),
// and in the Chicago font.
Do not use the TText::ReplaceStyles function to change the value of a particular style element unless you want all other styling information to be stripped from the character range.To modify the style information associated with a particular text range, you:
// Now modify the style information for a specific range within the text. TTextRange partialTextRange(12,TTextCount(1)); styledText.AddStyles(TFontPointSizeStyle(24), partialTextRange); // Text in the range partialTextRange is now red, 24 point, and in // the Chicago font. Style information for the other ranges is not changed.