Add and Assign style

Top  Previous  Next

In this demo we create a style (sty : TWPTextStyle;) by adding it to the style collection.


 sty := WPRTFProps1.ParStyles.AddStyle(NewStyleName.Text);


In case you do not use a TRTFProps component simply use this


 sty := WPRichText1.ParStyles.AddStyle(NewStyleName.Text);


Both does the same and is accessing the same ParStyles collection.


Then You can assign properties using the ASet methods:




 sty.ASet(WPAT_CharFontSize, 13 * 100);


To make the style scroller "see" the new style you need to add a





Please see reference for supported WPAT_ codes.


Many developers have asked why the styles do not have properties, such as sty.FontName?


The reason for this is that using functions to read and write the properties we can support the <undefined> state.


This means that if a font name was not set in a style we can react on that (by using the inherited font name). In general are the ASet... methods used to write a property while the AGet function are used to read it.


They usually require a var parameter which will be changed. The result value of an AGet function is TRUE if the property was defined, otherwise it is FALSE.


There is also the AGetDef() function which returns the property value itself, if it was not found it returns the default which was passed as parameter. (Read more about ASet)


To use the paragraph style we can set the name

 par.ABaseStyleName := NewStyleName.Text;


or the style directly:

 par.ABaseStyle := sty;


par is a reference to a TParagraph object. That can be WPRichText1.ActiveParagraph or WPRichText.FirstPar;


WPTools supports the definition of various paragraph attributes in styles, but no tabstops.


When you assign a style to a paragraph, you often cannot see the effect right away.


The reason for this is, that the paragraph itself and its text already defines certain attributes and so overrides the attributes which are defined in the style.


a) To remove the attributes which are defined by a style you can use the method SetStyle which can be used with a style number, a style name or the reference to an item of the ParStyles collection.

Two optional parameters control if the paragraph attributes and/or the character attributes should be adjusted to make all style attributes visible.    

  par.SetStyle(stylename  , true, true);


b) If you are working with selected text, you can use the procedure SelectedTextAttr.ClearAttr(false,true) to remove all styles from the selected text, only the paragraph styles will be used.


You can also use SelectedTextAttr.ClearAttrOverride - this will remove the attributes which are used in the paragraph styles. So the attributes which are not override attributes will be preserved, the attributes which will otherwise hide the paragraph style attributes will be removed.


c) You can also call par.ClearCharAttributes(true) to remove the character attributes from all characters.



After assigning a style the text should be reformatted. WPTools ∞ will take care that only the modified paragraphs are also initialized.



 WPRichText1.ActiveParagraph.ABaseStyleName := NewStyleName.Text;


 WPRichText1.ReformatAll(false, true);



This code will simply attach a style, it will not modify any properties of the paragraph or the text in this paragraph. So it is possible that the text does not use certain attributes of the style since they are overridden.


Usually when the complete text is selected and the selection is deleted, the attributes and style assignment are removed, too. This behavior can be disabled using the EditOptionEx2 wpClearSelectionDontRemoveParAttr