How to get Font Name of selected text in Rich Edit

Viewed 429

For some reason, I need to get the font names of selected text in TRichEdit. This code works fine if user is only using 1 font :

ShowMessage(redt1.SelAttributes.Name);

but now user is using more than 1 font. I scanned the properties of RichEdit but failed to find functions that will help me address the problem. Its already taking me some time. How can I get the fonts names of selected text?

1 Answers

I just created an RTF document with three lines of text, each in a different font.

The following code correctly lists the fonts used:

procedure TForm1.GetRTFFonts;
var
  i,
  Len : Integer;
  S : String;
begin
  Len := 0;
  for i := 0 to redt1.Lines.Count - 1 do begin
    redt1.SelStart := Len;
    S := redt1.SelAttributes.Name;
    if Memo1.Lines.IndexOf(S) < 0 then
      Memo1.Lines.Add(S);
    inc(Len, Length(redt1.Lines[i]) + 2);
  end;
end;

Logically, redt1.SelAttributes.Name can only name one font at a time, so to list all of the fonts in use, you would need to move the SelStart position one character at time through the document, rather than one line at a time. You could do this with a trivial addition to GetRTFFont above.

Equally, you could adapt the code to restrict it to the current selection in the RichEdit by saving the current selection start and its length, using the above method to examine only the characters in that range and then restoring the original selection.

Related