12

I am currently displaying some text in a TLabel. I would now like to emphasise certain words within the text by displaying them in bold or perhaps italic. What is the recommended way to do this in Delphi? Is there a TLabel-like component that can display simple HTML or markup? I am imagining some code like this:

label.text:='This information is <b>important</b>';

I am using Delphi 2010

awmross
  • 3,789
  • 3
  • 38
  • 51

5 Answers5

11

JvHTLabel in the JVCL will do the trick very nicely. Download the whole library from http://jvcl.delphi-jedi.org/

Rob McDonell
  • 1,309
  • 9
  • 15
  • Great suggestion. I didn't end up using it because, unlike TLabel, JvHTLabel doesn't support wordwrap if you want to display multiple lines. It worked otherwise. – awmross Dec 15 '10 at 01:47
5

Check the THtmlViewer components are free and compatible with delphi 2010.

RRUZ
  • 134,889
  • 20
  • 356
  • 483
  • 1
    I found this a little bulky for use as a single TLabel (it works, but I couldn't figure out how to make it transparent like a TLabel). In the end, I removed all the components on the form and replaced the whole thing with a single HTMLViewer component and did the layout / markup all in HTML. – awmross Dec 15 '10 at 01:52
3

One more:

Delphi Markup Label

The Delphi Markup Label is a label component for Delphi that supports some HTML-like tags to format the displayed text. Amongst others, it supports tags for hyperlinks, tabs, font size, color and styles. The full list of tags can be found in the accompanying test tool.

http://www.infintuary.org/stlabel.php Single pas file, compiled demo included. Some formatting is implemented via self-made tags so it won't be classic HTML. Has no transparence.

Community
  • 1
  • 1
Fr0sT
  • 2,959
  • 2
  • 25
  • 18
  • Replacing `TMDLabel = class (TCustomControl)` with `TMDLabel = class (TGraphicControl)` makes this label able to serve as background (on MDI parents for example). – Fr0sT Oct 21 '14 at 12:13
2

If you are already using a newer version of Delphi (at least the XE2 version), you can take a look at my TDzHTMLText component at: https://github.com/digao-dalpiaz/DzHTMLText

This component is a label with some HTML tags support, allowing you to format the text with Bold, Italic, Underline, Font Color, Font Size, Background Color, Tab Alignment, Text Alignment (left, center, right).

Check a print example:

DzHTMLText component example at design-time

The list below describes all possible tags to format label text:

<A[:abc]></A> - Link
<B></B> - Bold
<I></I> - Italic
<U></U> - Underline
<S></S> - Strike out
<FN:abc></FN> - Font Name
<FS:123></FS> - Font Size
<FC:clColor|$999999></FC> - Font Color
<BC:clColor|$999999></BC> - Background Color
<BR> - Line Break
<L></L> - Align Left
<C></C> - Align Center
<R></R> - Aligh Right
<T:123></T> - Tab
<TF:123></TF> - Tab with aligned break
  • Hey! Welcome to stack, it's nice to see you contributing :) a thought on improving this answer would be editing it to provide some examples of how you would use such component in code. – Smarticles101 Jan 24 '19 at 14:29
  • Hey! I reviewed the answer and completed with detailed instructions for tags use and a print example. I think now the answer is better. Thanks. – Digão Dalpiaz Jan 26 '19 at 17:26
1

Some ideas:

  1. Have a look at TLinkLabel in ExtCtrls. It lets you add links.. it paints the linked part underlined. Maybe you can steal the code and add support for other tags like <b> <i> <strong> <em>?

  2. You can use a TRichEdit component.. It's not HTML as you describe, but it should do the trick of formatting your text.

  3. Use TjanMarkupLabel from this page. "TjanMarkupLabel is a TGraphicControl descendant that renders its Text property in HTML style. Supported tags: Bold, Italic, Underline, Font (face, size and color) and Break."

Wouter van Nifterick
  • 23,603
  • 7
  • 78
  • 122