Querying and Modifying Styles in an IText Object

The Unicode Text Framework lets you access the styles associated with a given character or range, and add to or remove those styles.

If you specify a type of style to remove, removeStyles removes any style of that type. It does not look at the value of the style you specified to remove. For example, if you specify to remove ITextBoldfaceStyle(true), removeStyles removes any boldface style whether it is set to true or false.

This code shows how to query the styles at a particular point in the IText object someText and modify the style values:

offset_type runOffset;	

length_type runLength;



const ITextStyleSet* setPtr;

ITextPointSizeStyle size;



// Get the styles on the first style run.

// The offset and length of the run are returned.

setPtr = someText.stylesAt(0, runOffset, runLength)



// If the style run contains a point size style, increment its 
value by 3

if (setPtr.extract(size))

	size.setPointSize(size.pointSize() + 3);

// If the style run has no point size style, add one

else

	size.setPointSize(6);



// Apply the new point size style to the entire style run

someText.addStyles(size, runOffset, runLength);

To look for characters with specific styles, you can use IText::maximumStyleSpan. For example, this code shows how to look for all style runs of bold text, and underline those characters:

offset_type cursorOffset = 0;

offset_type spanOffset;

length_type spanLength;

ITextStyleSet set;

ITextBoldfaceStyle bold(true);

ITextUnderlinStyle line(true);



while (cursorOffset < someText.length()) {

// If the current character is boldface, underline the entire 
style run

	if (someText.maximumStyleSpan (cursorOffset, bold, 

		spanOffset, spanLength))

		someText.addStyles(line, spanOffset, spanLength);

	cursorOffset += spanLength;

}

You can also use ITextStyleRunIterator to access style runs.


Styles and Style Sets
Transcoding Classes
Text and Style Run Iteration

Iterating through Style Runs in an IText Object