Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- {
- changeSubStrFontColor in TRichEdit
- coded by: uragiristereo on august 28th, 2019
- usage example:
- resetStrFontColor(RichEdit1, clBlack);
- ^TRichedit ^TColor
- changeSubStrFontColor(RichEdit1, Edit1.Text, clRed);
- ^TRichedit ^TEdit.Text ^TColor
- }
- function subStrCount(const subStr: string; const str: string): Integer;
- begin
- if (Length(subStr)=0) or (Length(str)=0) or (Pos(subStr, str)=0) then
- Result:=0
- else
- Result:=(Length(str)-Length(StringReplace(str, subStr, '', [rfReplaceAll]))) div Length(subStr);
- end;
- function xPos(const subStr: string; const str: string; start: integer): Integer;
- var
- s: String;
- begin
- s:=Copy(str, start-1, Length(str));
- Result:=Pos(subStr, s)+start-1;
- end;
- procedure resetStrFontColor(const richEdit: TRichEdit; const color: TColor);
- var
- lastPos: Integer;
- begin
- lastPos:=richEdit.SelStart;
- richEdit.SelStart:=0;
- richEdit.SelLength:=Length(richEdit.Text);
- richEdit.SelAttributes.Color:=color;
- richEdit.SelStart:=lastPos;
- end;
- procedure changeSubStrFontColor(const richEdit: TRichEdit; const str: String; const color: TColor);
- var
- x, y, lastPos: Integer;
- begin
- y:=0;
- lastPos:=richEdit.SelStart;
- try
- for x:=1 to subStrCount(str, richEdit.Text) do begin
- if x=1 then
- richEdit.SelStart:=xPos(str, richEdit.Text, y)
- else
- richEdit.SelStart:=xPos(str, richEdit.Text, y)-2;
- richEdit.SelLength:=length(str);
- richEdit.SelAttributes.Color:=color;
- y:=richEdit.SelStart+length(str)+2;
- end;
- finally
- richEdit.SelStart:=lastPos;
- end;
- end;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement