[UPDATE]
To apply the style to a RichEdit already filled in the following algorithm could be used:
procedure ApplyStyleWhenMatchPattern(Edit: TRichEdit; const TokenStart,
TokenEnd: string; MatchColor: TColor);
var
StartPos, EndPos, OffSet, Len: Integer;
begin
Len:= Length(Edit.Text);
StartPos:= Edit.FindText(TokenStart, 0, Len, []);
EndPos:= Edit.FindText(TokenEnd, Succ(StartPos), Len, []);
while EndPos <> -1 do
begin
Edit.SelStart:= StartPos+ Length(TokenStart);
Edit.SelLength:= EndPos - StartPos -Length(TokenEnd) ;
Edit.SelAttributes.Color:= MatchColor;
OffSet:= Succ(EndPos);
StartPos:= Edit.FindText(TokenStart, OffSet, Len, []);
EndPos:= Edit.FindText(TokenEnd, Succ(OffSet), Len, []);
end;
end;
To apply effectively execute the method:
ApplyStylesWhenMatchPattern(RichEdit1, '->', '<-', clRed);
This algorithm does not provide an intersection of ->->
, if this is required, it should be reviewed.
To achieve your goal, you need to work with the properties SelAttributes
and SelText
of TRichEdit
. Encapsulate your Log function and do the following:
procedure LogIt(const AMessage: String);
begin
RichEdit1.SelAttributes.Color:= clBlack;
RichEdit1.SelText:= '->';
RichEdit1.SelAttributes.Color:= clRed;
RichEdit1.SelText:= AMessage;
RichEdit1.SelAttributes.Color:= clBlack;
RichEdit1.SelText:= '<-';
RichEdit1.Lines.Add('');
end;
You could still use the color of the text, for example, Error: Red, Warning: Yellow. In these cases, the above function would get a color.
procedure LogIt(const AMessage: String; AColor: TColor);
begin
RichEdit1.SelAttributes.Color:= clBlack;
RichEdit1.SelText:= '->';
RichEdit1.SelAttributes.Color:= AColor;
RichEdit1.SelText:= AMessage;
RichEdit1.SelAttributes.Color:= clBlack;
RichEdit1.SelText:= '<-';
RichEdit1.Lines.Add('');
end;