|
Fonts, Size, and Color
| [Previous] [Main] [Next] |
|
| |
| The HTML specification provides a number of tags for formatting text, but you may want to specify fonts, sizes, or colors different from the defaults used by browsers. The htmlFont class allows you to specify font face, size, and color, and corresponds to the HTML <FONT> tag.
| |
|
| |
| htmlFont inherits from htmlGroup, so you can add text and other objects to it. Any text contained by an htmlFont object will be rendered using the specified attributes.
| |
|
| |
| To change text color:
| |
|
| |
| htmlFont red_text ;
| |
| red_text.Color( COLOR_RED ) ;
| |
| red_text << "This is red text." ;
| |
| cout << red_text ;
| |
|
| |
| You can also specify font color in the constructor:
| |
|
| |
| htmlFont red_text( "", SIZE_DEFAULT, COLOR_RED ) ;
| |
| red_text << "This is red text." ;
| |
| cout << red_text ;
| |
|
| |
| You can also use the parentheses () operator to add text to a pre-formatted htmlFont object. This is particularly convenient for adding text in a single statement:
| |
|
| |
| htmlFont red_text( "Arial,Helvetica", SIZE_DEFAULT, COLOR_RED ) ;
| |
| cout << red_text( "This is red text." ) ;
| |
|
| |
| Several constructors are available for htmlFont. In the above example, the first argument to the constructor specifies the font face. Because we passed an empty string, html++ will use the browser default font face. The second argument specifies the size of the font (1=smallest, 7=largest). By specifying SIZE_DEFAULT, we're telling html++ to use the browser default size, so that there will be no change in size from surrounding text.
| |
|
| |
| To change the font face:
| |
|
| |
| htmlFont arial( "Arial" ) ;
| |
| arial << "This text uses the Arial font." ;
| |
|
| |
| If the particular font is not available on the browser's system, the browser may substitute a different font. Therefore, it's generally good practice to specify a list of alternate fonts that are acceptable. To do that, simply list the font faces separated by commans:
| |
|
| |
| htmlFont arial( "Arial, Helvetica, Verdana" ) ;
| |
| arial << "This text uses the Arial font. "
| |
| << "If Arial is not available, the browser "
| |
| << "will try Helvetica followed by Verdana." ;
| |
|
| |
| It's often useful to render the first character of a paragraph in a larger font than the rest of the text:
| |
|
| |
| htmlFont big( "", +2 ) ;
| |
| htmlParagraph p ;
| |
| p << (big << "T")
| |
| << "he first letter of this paragraph is bigger." ;
| |
|
| |
| Note how parentheses were used to add the letter T to the 'big' object before adding the result to the paragraph. Careful use of parentheses can make your code more elegant and readable. The above code snippet produces the following HTML code when 'p' is output to a page:
| |
|
| |
| <P><FONT SIZE="+2">T</FONT>he first letter of this paragraph is bigger.</P>
| |
|
| |
