Example: Creating and modifying styled text

The following examples show how to create a text instance, create and apply styling information, and change the styling information.

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:

  1. Instantiate the style object, assigning the value you want.
  2. Apply the style to the correct text range using the TText::AddStyles member function.
TText manages the TStyleSet objects that maintain the style information and creates a new TStyleSet instance if necessary.

To apply a set of styles to text simultaneously, you:

  1. Instantiate the style objects, assigning the values you want.
  2. Instantiate an empty TStyleSet object.
  3. Add each style object to the style set with the TStyleSet::Add function.
  4. Apply the style set to the correct text range using the TText::AddStyles function.
    If you apply character styles to a text object that contains no character data or to a range of length 0, they have no effect. If you apply paragraph styles to a range of length 0 (an insertion point), the styles are applied to the entire paragraph.
NOTE 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.

Modifying style data

After you apply a style set to a character range, you can:

TIP 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:

  1. Instantiate new style objects, assigning the modified values you want.
  2. Apply the new style objects to the correct text range.
    You can apply either a single style object or a set of style objects. The new style objects replace any existing style objects of the same type.
      // 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.

[Contents] [Previous] [Next]
Click the icon to mail questions or corrections about this material to Taligent personnel.
Copyright©1995 Taligent,Inc. All rights reserved.

Generated with WebMaker