Tech Tip: 4D Write Pro Range Length When Applying Styles
PRODUCT: 4D | VERSION: 18 | PLATFORM: Mac & Win
Published On: July 27, 2020
With the 4D Write Pro area a technique to apply styles to text is to toggle the styles desired for text to be entered where the cursor position is. For example first enter "The following text is " then toggle on Bold from the toolbar, then entering in "bold" will display:
When procedurally working with a 4D Write Pro document, this strategy does not work if the current section of the document already contains data. If there is no data in the current section (header, body, footer, ect.) then applying a style will have any data inserted procedurally use the style. If there is already data in the section and a Write Pro range of zero characters is applied a style, the style will not apply to any new data inserted.
The following code
$myWPDoc_o:=WP New $wpRange_o:=WP Text Range($myWPDoc_o;wk start text;wk start text) WP SET TEXT($wpRange_o;"The following text is ";wk replace) $wpRange_o:=WP Text Range($myWPDoc_o;wk end text;wk end text) WP SET ATTRIBUTES($wpRange_o;wk font bold;wk true) WP SET TEXT($wpRange_o;"bold";wk replace) |
will result in
Since the range was updated to the end of the text containing zero characters.
To apply the style, the data must be inserted first then have the style applied:
$myWPDoc_o:=WP New $wpRange_o:=WP Text Range($myWPDoc_o;wk start text;wk start text) WP SET TEXT($wpRange_o;"The following text is ";wk replace) $wpRange_o:=WP Text Range($myWPDoc_o;wk end text;wk end text) WP SET TEXT($wpRange_o;"bold";wk replace) WP SET ATTRIBUTES($wpRange_o;wk font bold;wk true) |
Another way is to insert at least a single dummy character to the range but the code must account for this and replace the character.
$myWPDoc_o:=WP New $wpRange_o:=WP Text Range($myWPDoc_o;wk start text;wk start text) WP SET TEXT($wpRange_o;"The following text is ";wk replace) $wpRange_o:=WP Text Range($myWPDoc_o;wk end text;wk end text) If($wpRange_o.start=$wpRange_o.end) WP SET TEXT($wpRange_o;" ") End if WP SET ATTRIBUTES($wpRange_o;wk font bold;wk true) WP SET TEXT($wpRange_o;"bold";wk replace) |