Recent

Author Topic: Highlight a known substring in a label  (Read 2828 times)

Zittergie

  • Full Member
  • ***
  • Posts: 114
    • XiX Music Player
Highlight a known substring in a label
« on: April 20, 2014, 08:02:13 pm »
Hi,

I would like to highlight a known substring in a label.

Eg.
Substring1:='Hello';
Substring2:='world!';
Label1.caption:=Substring1+' - '+Substring2;

When the mouse is above 'Hello', only 'Hello' must be highlighted.

The reason only one label is used, is that the label1 needs to be centered and may not exceed the Image used as backdrop.  If you use 2 labels, its is easy to highlight the text, but difficult to center the combined text.
Be the difference that makes a difference

Blaazen

  • Hero Member
  • *****
  • Posts: 3237
  • POKE 54296,15
    • Eye-Candy Controls
Re: Highlight a known substring in a label
« Reply #1 on: April 20, 2014, 08:28:39 pm »
I don't know how do this with only one label.

Try to put two labels on the panel. Anchor the left side of the left label to the left edge of the panel and anchor the left side of the right label to the left label. Then set Panel.AutoSize to True. And now you can center one panel. But this solution has no transparency.

Or can't you draw the text directly on the background image in Paint method or OnPaint event using Canvas.TextOut() or ThemeServices.DrawText() ? You can center two TextRects easily and store these two rectangles for later use with OnMouseMove event.
Lazarus 2.3.0 (rev main-2_3-2863...) FPC 3.3.1 x86_64-linux-qt Chakra, Qt 4.8.7/5.13.2, Plasma 5.17.3
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

Zittergie

  • Full Member
  • ***
  • Posts: 114
    • XiX Music Player
Re: Highlight a known substring in a label
« Reply #2 on: April 21, 2014, 08:33:42 pm »
Hi Blaazen,

reading your answer brought me to this:

- Center the label
- Set the constraints.maxwidth to the maxsize
- Put a label with the substring1 on the left of the label (This makes the font a little bit heavier) so the next step is needed
- Put a label with the substring2 on the right of the label

See example http://www.xixmusicplayer.org/download/xix.mov
Be the difference that makes a difference

Blaazen

  • Hero Member
  • *****
  • Posts: 3237
  • POKE 54296,15
    • Eye-Candy Controls
Re: Highlight a known substring in a label
« Reply #3 on: April 21, 2014, 09:03:10 pm »
IMO it mainly depends if you need transparency. If the labels will be on the uniform background then it does not matter but if they will be over some image then it should not work on all platforms (AFAIK GTK2 labels are not transparent).
Lazarus 2.3.0 (rev main-2_3-2863...) FPC 3.3.1 x86_64-linux-qt Chakra, Qt 4.8.7/5.13.2, Plasma 5.17.3
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

Zittergie

  • Full Member
  • ***
  • Posts: 114
    • XiX Music Player
Re: Highlight a known substring in a label
« Reply #4 on: April 21, 2014, 09:08:53 pm »
I use GTK2 on Linux (intel & arm) and the labels are transparent.  So no problems there.
Would be Nice to have a label component like on QT, that can be formatted with Html.
Be the difference that makes a difference

Blaazen

  • Hero Member
  • *****
  • Posts: 3237
  • POKE 54296,15
    • Eye-Candy Controls
Re: Highlight a known substring in a label
« Reply #5 on: April 21, 2014, 09:28:43 pm »
I mistaken, both TLabel and TStaticText are transparent on GTK2 and Qt4 as well.
Lazarus 2.3.0 (rev main-2_3-2863...) FPC 3.3.1 x86_64-linux-qt Chakra, Qt 4.8.7/5.13.2, Plasma 5.17.3
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

 

TinyPortal © 2005-2018