Coloring text inside "tag"

0

Well, I have a Rich edit and am using it as a changelog, and I would like all text with you in ->, to be of a specific color. Example, in:

->10/10/2014<-

10/10/2014 would be a specific color. How can I do this? Ps: I'm using idhttp to download the log:

sRichEdit1.Text := IdHTTP2.Get('www.blabla.com/changelog.txt');
    
asked by anonymous 20.10.2014 / 17:24

1 answer

3

[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;
    
20.10.2014 / 18:11