-2

I am using Delphi XE3. I need to set some parts of a long statement to bold and red. Since TLabel does not support such a feature, while merge several TLabels is rather inconvenient, and TJvHTLabel does not support bold, I decide to implement that via a TRichEdit control, by setting:

  1. BorderStyle to bsNone.
  2. ParentColor to true.
  3. ReadOnly to true.

Then use the following code to set 2 characters to bold:

  reMsg.SelStart := 2;
  reMsg.SelLength := 2;
  reMsg.SelAttributes.Style := [fsBold];
  reMsg.SelStart := 0;
  reMsg.SelLength := 0;

But I find the font of the rich edit control will changes to a strange one. Below is a comparison of the tlabel control and trichedit control:

enter image description here

You can see in the second caption, the font is changed.

I try many methods, such as set the font of the richedit control manually:

reMsg.Font.Name := labMsg.Font.Name;
reMsg.Font.Charset := labMsg.Font.Charset;

But still cannot solve the problem.

Why?

alancc
  • 487
  • 2
  • 24
  • 68
  • It seems like `FontPitch` property of `reMsg.Font` is changed too... Did you try `reMsg.Font.Assign(labMsg.Font)`? – Miamy Nov 04 '18 at 09:54

1 Answers1

-1

Perhaps, you should set your TRichEdit's property ParentFont to true. In this case TRichEdit will use font of its parent (f.e. of TForm). After this apply your code to make part of text bold and you will see this:

On this image "is" in word "this" output with bold font style

I didn't modify Font property neither of TForm nor TRichEdit. Just created new project and placed component on form. Because all work as expected, I suppose that the problem described in your question is related with manually modified TRichEdit's Font property. Just get back default values of TRichEdit's font or set its ParentFont property to true.

As an additional solution, instead of using TRichEdit for this purpose you'd better to look at TMDLabel. It has lots of features to play with text style, color, size etc.

I don't know name of author of this nice component, but there is his web-site: Infintuary.org

For example, with this pseudo-HTML-code you can "paint" text as needed:

<fs:14><fc:clRed>This</fc> is a <fc:clBlue><b>test</b> caption</fc> for TLabel</fs>

After putting this text in TMDLabel you will get this (screenshot from demo-app downloaded from official web-site):

enter image description here

There is also demo-app which explains how to use this component and what tags it supports. Worth to try in my opinion.

Josef Švejk
  • 1,047
  • 2
  • 12
  • 23
  • This does not answer the question that was asked. As you know, recommendation questions are off topic, and since this answer simply recommends a component, it stands to reason that it doesn't fit. – David Heffernan Nov 05 '18 at 10:01
  • @DavidHeffernan thank you! I've expanded my answer to explain what to do to avoid effect that OP observed. – Josef Švejk Nov 05 '18 at 16:16
  • Now the problem is that the behaviour reported in the question can't be reproduced. Which means the question should be closed rather than answered. – David Heffernan Nov 05 '18 at 16:53
  • @DavidHeffernan does it a case not to answer if one can't reproduce described behaviour? – Josef Švejk Nov 05 '18 at 17:02
  • Of course. You can't answer a question that isn't well posed. If you do, expect down votes. – David Heffernan Nov 05 '18 at 17:28
  • @DavidHeffernan I thought that any answer that gets the asker going in the right direction is helpful. – Josef Švejk Nov 05 '18 at 17:40
  • You could do that in comments. Answers are meant to focus on the question above all. If there is no well defined question there can be no answer. – David Heffernan Nov 05 '18 at 17:53