Search code examples
delphirichedit

Make the first 4 characters in a richedit line bold


How can I make the first 4 characters in a line I am going to add bold?
Example:

richedit1.Lines.Add('Test123');

I want Test to be bold but leave 123 normal.

Can someone help me?


Solution

  • Try something like this:

    procedure TForm1.AddFormattedText(const AText: string; AStyle: TFontStyles);
    begin
      RichEdit1.SelStart := RichEdit1.GetTextLen;
      RichEdit1.SelLength := 0;
      RichEdit1.SelAttributes.Style := AStyle;
      RichEdit1.SelText := AText;
    end;
    

    AddFormattedText('Test', [fsBold]);
    AddFormattedText('123'+sLineBreak, []);