Display simple markup in a Label in Delphi - delphi

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

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

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

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.

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

Some ideas:
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>?
You can use a TRichEdit component.. It's not HTML as you describe, but it should do the trick of formatting your text.
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."

Related

What is this Text Styles thing in Interface Builder?

Wondering if there is some way to modify the properties found in this Text Styles dropdown on interface builder. Not sure how to find any docs relating to this.
Text Styles
Text Styles are semantic descriptions.
There are a fixed number of descriptions, and currently, in iOS 9,
10 styles are supported:
Title1, Title2, Title3
Headline, Subheadline
Body
Footnote
Caption1, Caption2
Callout
Use this link for reference
https://developer.apple.com/reference/uikit/uifontdescriptor#//apple_ref/doc/constant_group/Text_Styles

Make TMemo show partially visible lines

Is it possible to make TMemo show partially visible lines? I'm looking for a native way to do that, without custom rendering.
I made an example screenshot for those who not familiar with this issue:
As you can see,, in the area I marked with red, there is enough space to display some part of the next line that is not visible by default.
There are some components, like TListBox, which show partially visible lines, so I want to make TMemo do the same.
P.S. I'm not interested in other suggestions, like enlarging the TMemo size, that's off-topic.
It's not possible to make a TMemo to show partial lines. For what you can do natively to a memo, which is a multiline edit control, see edit controls on MSDN.
You could use a TRichEdit instead.
You can access the Lines and Text properties, just like for a TMemo. It also has the WordWrap, ReadOnly and ScrollBars properties, just like TMemo, so it could be a drop-in replacement, but showing partial lines at the bottom as required.
Tested in Delphi 10.1 Berlin as well.

Removing Formatting from Text Box Controls - InfoPath

I'm working in InfoPath and somehow created about 16 Text Box Controls with formatting that I cannot override in terms of text size as well as strikethroughs. Any suggestions to be able to remove the formatting and be able to maintain these controls?
It sounds like you got into the code and accessed the toolbar. If you right click on the textbox and go to properties it should take you to the properties window. Make sure under the space next to "Text" in the properties window is blank.

How to change caption font color on TCheckBox, TRadioButton, TGroupBox?

I don't know what my problem is, but I cannot set the font color in DEx2 for controls like TCheckBox, TRadioButton, TGroubBox, and TRadioGroup. It doesn't matter if I do it in the IDE or programmatically.
I have set my form color to clBlack and want my captions to be clWhite, but they won't render any color but clBlack. When I assign a color on a form's OnShow event and step through it in the debugger, it shows the value I assigned it, but on the screen it is still black.
I am not using styles or anything else. Any ideas?
Here is a sample form:
white on black example http://www.skippix.us/temp/Delphi-Font-Problem.bmp
When you uses the windows themes in an application, most of the custom settings like the font colors are ignored. As workaround and depending of the component you can ownerdraw the control (only when this feature is supported), override the paint method in order to use your own color in the font (TRadioGroup, TGroupBox), and for components like TCheckBox and TRadioButton (which are WinAPI controls wrappers) you must intercept the WM_PAINT windows message and implement your own code to draw the control.
Also starting with Delphi-xe2 you can use the vcl styles which allow you to change the appearance of the controls, from here (and when is possible) you can modify the style hooks to apply your own font colors and other customizations.
A simple and easier workaround is to create a checkbox without caption and add a label after it. You can easily change the label's color. You can also create a new component that binds a label to the checkbox itself. That's what worked for me on Delphi 2007.
It will work under the following setting:
Project Options > Application > Runtime Themes = none

How use different font styles and icon in a Firemonkey stringgrid cell

There seems to be not much information available about this subject so hopefully someone can give some suggestions.
I would like to change color and style of different parts of text in a stringgrid cell.
Example:
20-02-2011 - Document Title
Would it also be possible to show an icon in a stringgrid cell?
It is indeed possible to show icons or bitmaps in a string grid, the Monkey Styler blog includes an example of how to customise cell display in a FMX grid.
http://monkeystyler.com/blog/entry/firemonkey-grid-basics-custom-cells-and-columns
Thinking about the other part of your question though, you will have to build a style up to display text in different fonts, as yet FMX does not have a richtext equivalent or HTML rendering components (I'm sure the latter will come, the former I am not confident on getting for a while!). You would have to have a text component in the style for each different font size/style etc, you would need to name the text elements (using binding or stylename properties) and handle the setting of the text contents using code in the ongetvalue and onsetvalue events (I guess you could also use a onapplystyle event) to display some text in bold and some not.
The event handler would then use either a binding - which is easier to set but - in my experience - buggy;
mycell.binding['boldtext']:='sometext';
Or find a style element by stylename by using findstyleresource;
tempobj:=mycell.findstyleresource('boldtext');
if tempobj is ttext then
ttext(tempobj).text='sometext';
Excuse any bad coding style there, but you get the general idea (I hope) if you set autosize to true for the ttext components in the style and align them left you get - mostly - what you are after. The only issue is that the resulting text doesn't look quite seamless because autosizing and aligning left doesn't really give you the same space between the fonts that you would get with a richtext or HTML display. You should be able to fiddle with the padding (giving it a negative number for the left padding should allow you to remove any extra space) but you have to live with that not being entirely perfect compared to proper font hinting and alignment.

Resources