2

I try to add text containing a non-breaking space (Unicode U+00A0) to a RichEdit with property Wordwrap = True. I use the following code:

RichEdit.Lines.Add('Some text some text some text 1000' + #160 + 'km some text');

This gives a space between '1000' and 'km', but it is not non-breaking: When changing the width of the RichEdit a linebreak may occur here, like with a regular space. I have Windows 10 version 2004.

Am I doing something wrong, or is this a general limitation of the RichEdit component?

Matej
  • 442
  • 3
  • 9
  • This will indeed put a U+00A0 between 1000 and km; if I copy the text from the Rich Edit control at runtime I can see it. However, the version of the Rich Edit control you get by using the `TRichEdit` wrapper is a very old one, and I suspect it doesn't support nbsp. Did you try https://stackoverflow.com/questions/26960672/how-do-i-use-the-latest-richedit-with-delphi-2007? – Andreas Rejbrand Mar 21 '21 at 22:28
  • No, that didn't work. – Andreas Rejbrand Mar 21 '21 at 22:41
  • You can look at the documentation of the OS control to see if you find anything interesting: https://learn.microsoft.com/en-us/windows/win32/controls/rich-edit-controls – Andreas Rejbrand Mar 21 '21 at 23:02
  • Delphi 10.3 still uses the old RichEdit 2.0. A method how to use the newer RichEdit 4.1 is described at http://fgaillard.com/2010/09/using-richedit-4-1-with-d2010/. However, this does not help as RichEdit 4.1 still ignores non-breaking spaces. The documentation at https://learn.microsoft.com/en-us/windows/win32/controls/about-rich-edit-controls#unsupported-edit-control-functionality does not mention any support for non-breaking spaces. So, this seems to be a general limitation of the RichEdit component. – Matej Mar 22 '21 at 10:29
  • 1
    I also tried to use 4.1 (and it did work -- for instance, I was able to display fancy tables) and can confirm that that upgrade alone doesn't make nbsp work. However, I notice that nbsp works in WordPad, so there might still be a way. For instance, https://learn.microsoft.com/en-us/windows/win32/controls/use-word-and-line-break-information might be worth investigating, at least if you will only ever use English (or at least Latin-based) text. – Andreas Rejbrand Mar 22 '21 at 10:32

1 Answers1

0

The best way I found to achieve this is to use a procedure that will replace the no break space with a hidden character:

procedure TForm1.AddLine(txt : string);
var
  OrigColour : TColor;
  p : integer;
begin
  p := Pos(#160, txt);

  if p > 0 then
  begin
    OrigColour := RichEdit1.Font.Color;
    RichEdit1.SelText := Copy(txt, 0, p-1);
    RichEdit1.SelAttributes.Color := RichEdit1.Color;
    RichEdit1.SelText := 'o';
    RichEdit1.SelAttributes.Color := OrigColour;
    RichEdit1.SelText := Copy(txt, p+1, Length(txt) - (p)) + #13;
  end
  else
    RichEdit1.Lines.Add(txt);
end;

You can then use it like this:

  AddLine('Some text some text some text 1000' + #160 + 'km some text');

And that will stop the 'km' wrapping without the '1000'.

I've used a lower case 'o'in the example. Avoid underscores or hyphens etc. as they are characters that wrapping will break on.

My solution is fine for displaying the text, but if you are going to do something with it you might need to strip out any hidden characters.

The hidden characters will also show up if you highlight the text.

Mick
  • 141
  • 9