4
votes

I have a small Delphi 10.3.3 app that has some text editing functions, using a TMemo where the user type the text.

I'm trying to include some formatting options, something as this site provides :

http://qaz.wtf/u/convert.cgi?text=How%20do%20it%20on%20Delphi

When i copy the 'circled' text from the site above and paste on my memo, it works, appears 'circled'. But i want to give my user the ability to apply the formatting inside my app.

For instance, i want to have a speedbutton to apply the 'circle' formatting to the current TMemo selected text : the user selects a text , click on this speedbutton and then the selected text gets the 'circled' formatting.

enter image description here

1
The VCL memo doesn't support formatting. You need an RTF edit control.David Heffernan
@DavidHeffernan How can it not support, if when i paste the formatted text from the site above, it will show on the memo ?delphirules
@David: You do know this has nothing to do with formatting? It's all about replacing one character with another.Andreas Rejbrand
Clearly I misunderstood what you want to do.David Heffernan
@DavidHeffernan No worries, thanks for trying to help anyway.delphirules

1 Answers

4
votes

This is rather easy. If you look at the Unicode chart for the enclosed alphanumerics, you realise that the following mapping is valid:

function EncircleChr(AChr: Char): Char;
begin
  case AChr of
    '0':
      Result := Chr($24EA);
    '1'..'9':
      Result := Chr($2460 + Ord(AChr) - Ord('1'));
    'a'..'z':
      Result := Chr($24D0 + Ord(AChr) - Ord('a'));
    'A'..'Z':
      Result := Chr($24B6 + Ord(AChr) - Ord('A'));
  else
    Result := AChr;
  end;
end;

Hence, with

function Encircle(const S: string): string;
var
  i: Integer;
begin
  SetLength(Result, S.Length);
  for i := 1 to S.Length do
    Result[i] := EncircleChr(S[i]);
end;

and

procedure TForm1.Button1Click(Sender: TObject);
begin
  Memo1.SelText := Encircle(Memo1.SelText);
end;

you get the desired behaviour:

Screenshot of a TMemo with letters and digits transformed into their encircled versions.