If you need mixed coloring then this is an example that will add a new line with several different colored words to TRichMemo:
Example:
Black, Green, Blue, RedBlack, Green, Blue, RedBlack, Green, Blue, RedSince none of forum examples worked for me, I found this working on Windows:
procedure TForm1.Button1Click(Sender: TObject);
procedure AddColorStr(s: string; const col: TColor = clBlack; const NewLine: boolean = true);
begin
with RichMemo1 do
begin
if NewLine then
begin
Lines.Add('');
Lines.Delete(Lines.Count - 1); // avoid double line spacing
end;
SelStart := Length(Text);
SelText := s;
SelLength := Length(s);
SetRangeColor(SelStart, SelLength, col);
// deselect inserted string and position cursor at the end of the text
SelStart := Length(Text);
SelText := '';
end;
end;
begin
AddColorStr('Black, ');
AddColorStr('Green, ', clGReen, false);
AddColorStr('Blue, ', clBlue, false);
AddColorStr('Red', clRed, false);
end;
Hopefully someone having the same problem will find it, or decide to extend TRichMemo with a method that appends colored text...
Here is step by step tutorial:
http://lazplanet.blogspot.com/2013/12/create-rich-text-editor-for-yourself.htmlUPDATE1: I have just updated the Wiki. It would be nice if someone could test if this works on any other OS.
UPDATE2: Two more examples added:
http://wiki.freepascal.org/RichMemo#Append_mixed_color_text_at_the_end_of_the_RichMemoUPDATE3: Fixed scrolling error by changing example. Added link to blog tutorial.