Work with SPAN Styles

<< Click to Display Table of Contents >>

Navigation:  Programming > Change text attributes in code > Styles (CSS) >

Work with SPAN Styles

Especially to provide strong HTML support WPTools can work with embedded SPAN styles. This are special objects which are embedded into the text. They are always used in pairs, one opening and one closing. The opening object can select a style and the closing object selects the previous settings again.

 

If you are working with RTF you should not use SPAN objects.

 

In this demo we create the objects right in the TParagraph object. If you need a less abstract way to work with the objects please check out the 'Code' procedures implemented in TWPRichText. They make it easy to create the objects, for example wrap selected text into a pair of SPAN tags.

 

 par := WPRichText1.ActiveText.AppendPar;

 pos := par.Insert(0, '3) SECOND STYLE - except for ',0);

 spanobj_open := par.InsertNewObject(pos,wpobjSPANStyle, true, false);

 spanobj_open.StyleName := 'FIRST';

 inc(pos);

 pos := par.Insert(pos, '<SPAN "FIRST">',0);

 spanobj_close := par.InsertNewObject(pos,wpobjSPANStyle, true, true);

 spanobj_close.SetTag(spanobj_open.NewTag); //<-- tags are used to link start with end!

 inc(pos);

 pos := par.Insert(pos, ' - END',0);

 par.ABaseStyleName := 'SECOND';

 

The variables spanobj_open and spanobj_close are of type TWPTextObj. They are created by InsertNewObject which requires four parameters:

index: Integer;

objtype: TWPTextObjType;

HasClosing, IsClosing: Boolean

 

Please note how the value for 'IsClosing' is alternated the the demo source code. "pos" is an integer value which is the index into the paragraph. The insert() function always returns the position after the inserted text, so pos is always incremented.

 

Please note, that for each open SPAN object the closing object must be on the same paragraph. If you need a linebreak you can use the code Char(10) to create a soft line break.

 

Usually SPAN objects are not visible. They can be made visible using the flag wpShowSPANCodes in property Formatoptions. The display is controlled by property SPANObjectTextAttr, the text can be changed using SPANObjectTextAttr.CodeOpeningText and SPANObjectTextAttr.CodeClosingText (%Y inserts the StyleName).