Coloring text within "tag"

Asked

Viewed 269 times

0

Well, I have a Rich Edit and I’m using it as a changelog, and I’d like every text with you in -><-to be of a specific color. For example, in:

->10/10/2014<-

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

sRichEdit1.Text := IdHTTP2.Get('www.blabla.com/changelog.txt');

1 answer

3


[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.

  • I’ll post another answer

  • 2

    Put another question, because the algorithm is totally different, for example. It can appear 2 times "->asdas-<" on the same line?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.