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:
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);
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);
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);
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);
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);
We can make the text bold using setBold().
$p = new Paragraph();
$textStyle = new TextStyle();
$textStyle->setBold();
$p->addText('Bold text.', $textStyle);
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);
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);
We can make the text italic using setItalic().
$p = new Paragraph();
$textStyle = new TextStyle();
$textStyle->setItalic();
$p->addText('Italic text.', $textStyle);
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);
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);
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);
We use setUnderlineWordMode($mode) to specify whether underlining is applied to words only or to portions of text.
There are two modes:
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);
We can make the text blink using setTextBlinking().
$p = new Paragraph();
$textStyle = new TextStyle();
$textStyle->setTextBlinking();
$p->addText('Blinking text', $textStyle);
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);
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);