Text style

The TextStyle class is used to change the style properties of a text added to a paragraph.

Here are the properties that can be changed:

Text transformation

Using the method setTextTransform($transform), we can transform the text to uppercase, lowercase, or capitalization.

The argument can have one of these constants fro the class StyleConstants: StyleConstants::UPPER_CASE, StyleConstants::LOWER_CASE or StyleConstants::CAPITALIZE.

$p = new Paragraph();
$textStyle = new TextStyle();
$textStyle->setTextTransform(StyleConstants::UPPER_CASE);
$p->addText('This text will be in upper case', $textStyle);

Color

Using the method setColor($color), we can change the color of the text. Note that the color must be in the hexadecimal notation.

$p = new Paragraph();
$textStyle = new TextStyle();
$textStyle->setColor('#ff0000'); //Change the color to red
$p->addText('A bloody phrase.', $textStyle);    

Text outline

We can display the outline of text instead of the text itself using the method setTextOutline().

$p = new Paragraph();
$textStyle = new TextStyle();
$textStyle->setTextOutline();
$p->addText('Outlined text.', $textStyle);    

Line through

If we want to draw a line through the text, we can use the method setLineThrough($lineType, $lineWidth).

  • $lineType has two possible values: StyleConstants::SINGLE (default) or StyleConstants::DOUBLE, to draw two lines.
  • $lineWidth has also two possible values: StyleConstants::NORMAL (default) or StyleConstants::BOLD.

$p = new Paragraph();
$textStyle = new TextStyle();
$textStyle->setLineThrough();
$p->addText('Single line.', $textStyle);

$textStyle2 = new TextStyle();
$textStyle2->setLineThrough(StyleConstants::DOUBLE);
$p->addText('Double line.', $textStyle2);    

Linethrough word mode

We use setLineThroughWordMode($mode) to specify whether lining through is applied to words only or to portions of text.

There are two modes:

  • StyleConstants::CONTINUOUS: The spaces between words and the words are lined through.
  • StyleConstants::SKIP_WHITE_SPACE: Only the words are lined through

$p = new Paragraph();
$textStyle = new TextStyle();
$textStyle->setLineThrough();
$textStyle->setLineThroughWordMode(StyleConstants::SKIP_WHITE_SPACE);
$p->addText('Hello World!!', $textStyle);    

Bold

We can make the text bold using setBold().

$p = new Paragraph();
$textStyle = new TextStyle();
$textStyle->setBold();
$p->addText('Bold text.', $textStyle);    

Text position

We can specify whether text is positioned above or below the baseline using setTextPosition($position). The position can be either StyleConstants::SUPER or StyleConstants::SUB.

$p = new Paragraph();
$p->addText('Some text.');
$textStyle = new TextStyle();
$textStyle->setTextPosition(StyleConstants::SUPER);
$p->addText('super text.', $textStyle);    

Font size

We can change the font size using setFontSize($size).

The size can be a percentage, or in one of these units (cm, mm, in, pt, px, em), or just a number (no idea how it is calculated).

$p = new Paragraph();
$t = new TextStyle();
$t->setFontSize('20pt');
$p->addText('20pt font-size', $t);    

Italic

We can make the text italic using setItalic().

$p = new Paragraph();
$textStyle = new TextStyle();
$textStyle->setItalic();
$p->addText('Italic text.', $textStyle);    

Font relief

Using the method setFontRelief($fontRelief), we can specify whether the font should be embossed or engraved.

$fontRelief can have one of two values: StyleConstants::EMBOSSED or StyleConstants::ENGRAVED.

$p = new Paragraph();
$textStyle = new TextStyle();
$textStyle->setFontSize(20);
$textStyle->setFontRelief(StyleConstants::EMBOSSED);
$p->addText('EMBOSSED text.', $textStyle);    

Text shadow

We create a text shadow using setTextShadow().

$p = new Paragraph();
$textStyle = new TextStyle();
$textStyle->setFontSize(20);
$textStyle->setFontRelief(StyleConstants::EMBOSSED);
$p->addText('EMBOSSED text.', $textStyle); 

Text underline

We can underline a text using setTextUnderline($underlineType, $underlineStyle, $underlineWidth, $underlineColor). Note that all argument are optional.

  • $underlineType: Type of the underline: simple (default) or double; SIMPLE/DOUBLE
  • $underlineStyle: How the line is rendered, can be: SOLID (default), DOTTED, DASH, LONG_DASH, DOT_DOT_DASH, WAVE.
  • $underlineWidth: Width of the line: AUTO, NORMAL (default), BOLD, THIN, MEDIUM, THICK.
  • $underlineColor: Color of the underline (hexadecimal notation).
$p = new Paragraph();
$textStyle1 = new TextStyle();
//single, solid black underline
$textStyle1->setTextUnderline();
$p->addText('Hello World!!', $textStyle1);

$textStyle2 = new TextStyle();
//double, solid black underline
$textStyle2->setTextUnderline(StyleConstants::DOUBLE); 
$p->addText('Hello World!!', $textStyle2);    

Underline word mode

We use setUnderlineWordMode($mode) to specify whether underlining is applied to words only or to portions of text.

There are two modes:

  • StyleConstants::CONTINUOUS: The spaces between words and the words are underlined.
  • StyleConstants::SKIP_WHITE_SPACE: Only the words are underlined

Of course, the text need to be underlined first, using setTextUnderline().

$p = new Paragraph();
$textStyle = new TextStyle();
$textStyle->setTextUnderline();
$textStyle->setUnderlineWordMode(StyleConstants::SKIP_WHITE_SPACE);
$p->addText('Hello World!!', $textStyle);    

Text blinking

We can make the text blink using setTextBlinking().

$p = new Paragraph();
$textStyle = new TextStyle();
$textStyle->setTextBlinking();
$p->addText('Blinking text', $textStyle);    

Text background color

We can change the background color of the text using setTextBackgroundColor($color).

$p = new Paragraph();
$textStyle = new TextStyle();
$textStyle->setTextBackgroundColor('#ff0000');
$p->addText('Some text', $textStyle);    

Rotation angle

We can rotate the text by a certain angle using setRotationAngle($angle). The possible angles are: 0, 90 and 270.

$p = new Paragraph();
$textStyle = new TextStyle();
$textStyle->setRotationAngle(90);
$p->addText('Rotated text', $textStyle);