Creating a Styled Text String

To add styles to characters or paragraphs in an IText object, create the appropriate styles with the correct values and apply them to a specific character range:

  1. Instantiate a style object for the style you want to apply, passing in an appropriate value.
  2. (Optional) Add the styles to a style set.
  3. Call the IText::addStyles function, specifying the range of characters to apply the styles to.

For example:

IText styledString("Hello World!");

// Create a style set

ITextStyleSet stylesToAdd;

// Add styles to the style set

stylesToAdd.add(ITextPointSizeStyle(12));

stylesToAdd.add(ITextTypefaceStyle("Courier");

stylesToAdd.add(ITextUnderlineStyle(true));

// Apply styles to the entire string

styledString.addStyles(stylesToAdd, 0, IText::npos);

// Apply another style only to the first word

styledString.addStyles(ITextUnderlineStyle(true), 0, 5);

You apply paragraph styles the same way, except you don't have to specify the character range exactly. Specify one or more characters in the paragraph you want to apply styles to, or specify a character range extending across multiple paragraphs. IText automatically extends the paragraph styles to apply to all paragraphs that contain any character offsets you specify. For example:

IParagraphJustificationStyle 
pstyle(IParagraphJustificationStyle::kCenter);

styledString.addStyles(pstyles, 0, 1);