[UPDATING]
To apply the style to an already filled Richedit 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 effectively implement the method:
ApplyStylesWhenMatchPattern(RichEdit1, '->', '<-', clRed);
This algorithm does not provide for command intersection ->->
, if such functionality is necessary, it should be reviewed.
To achieve your goal, you need to work with the properties SelAttributes
and SelText
of TRichEdit
. Package your Log function and do as follows:
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 also use the text color custom, example, for Error: Red, Warning: Yellow. In such cases, the above function would receive 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;
In this case, what if the text is already written? The changelog is downloaded from the server, so it is already written.
– Gabriel Sales
I’ll post another answer
– EProgrammerNotFound
Put another question, because the algorithm is totally different, for example. It can appear 2 times "->asdas-<" on the same line?
– EProgrammerNotFound