Iterating through Style Runs in an IText Object

The Unicode Text Framework provides ITextStyleRunIterator for iterating through the style runs in an IText object. To use ITextStyleRunIterator:

  1. To create the ITextStyleRunIterator, pass the constructor the IText object you want to iterate over.
  2. Use the iterator operators ++ and -- to move forward and backward through style runs.
  3. Extract the styles on the current style run. The iterator's operator-> lets you call the ITextStyleSet::extract function for the current style run's style set.
  4. Use the ITextStyleRunIterator functions runStart and runLength to get the extent of the current style run.

For example, this code shows how to use an iterator to modify the point size for each style run, or add a point-size style if there is none:

ITextPointSizeStyle size;

ITextStyleRunIterator iter(someText);

for ( ; iter; ++iter) {

	if (iter->extract(size))

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

	else

		size.setPointSize(6);

	someText.addStyles(size, iter.runStart(), iter.runLength());

}