Changing Font from inserted Hyperlink

General TRichView support forum. Please post your questions here
Post Reply
tomr
Posts: 36
Joined: Wed Dec 09, 2020 9:36 am

Changing Font from inserted Hyperlink

Post by tomr »

Hello RichView Support,
I'd like to customize certain font properties of hyperlinks that are pasted into the RichView editor. My goal is to apply these formatting changes only when a hyperlink is inserted via the paste operation.
Currently, I’m using the following code within the OnPaste event of the RichViewEdit:
Changing Hyperlink Font.png
Changing Hyperlink Font.png (27.68 KiB) Viewed 5403 times
However, with this approach, I'm encountering issues when inserting images immediately after hyperlinks — RichView seems to behave unexpectedly in this scenario.

Am I overlooking something, or is the OnPaste event not the right place to apply these changes? Any advice on how to better handle hyperlink formatting during paste operations would be greatly appreciated.
Sergey Tkachenko
Site Admin
Posts: 17878
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Re: Changing Font from inserted Hyperlink

Post by Sergey Tkachenko »

I don't understand what problems this code causes with images.

However, this code has problems.
1) You change properties of a text style, but this style may be used in different places of the document. This is not what the user might expect.
2) This code may lead to adding a new text style on each call, although all properties of new styles may be identical (because GetHtyperlinkStyleNo tries to reuse an existing style, but then you change its properties, so reusing will not work). I recommend assigning all properties manually and use RVStyle.FindTextStyle

TRichViewEdit supports a special hyperlink clipboard format, but modern applications do not use it anymore.
They copy a link target as a plain text. A special hyperlink format was used by Internet Explorer. In any case, IE also copied a URL as a text, so we can safely use OnPaste to check hyperlinks in a pasted text.

The code may be like this:

Code: Select all

use Clipbrd, RVFileFuncs;

procedure TForm3.RichViewEdit1Paste(Sender: TCustomRichViewEdit; var DoDefault:
    Boolean);
var
  S: UnicodeString;
  TextStyle: TFontInfo;
  StyleNo: Integer;
begin
  if not Clipboard.HasFormat(CF_UNICODETEXT) then
    exit;
  S := Clipboard.AsText;
  if not RVIsURL(S) then
    exit;
  TextStyle := TFontInfo.Create(nil);
  try
    StyleNo := Sender.CurTextStyleNo;
    TextStyle.Assign(Sender.Style.TextStyles[StyleNo]);
    // change to your properties:
    TextStyle.Jump := True;
    TextStyle.Style := [fsUnderline, fsBold];
    TextStyle.Color := clRed;
    // end of custom properties
    if not Sender.Style.TextStyles[StyleNo].Jump then
      TextStyle.NextStyleNo := StyleNo;
    Sender.CurTextStyleNo := Sender.Style.FindTextStyle(TextStyle);
    Sender.InsertStringWTag(S, S);
    Sender.CurTextStyleNo := TextStyle.NextStyleNo;
    DoDefault := False;
  finally
    TextStyle.Free;
  end;

end;
tomr
Posts: 36
Joined: Wed Dec 09, 2020 9:36 am

Re: Changing Font from inserted Hyperlink

Post by tomr »

Hi Sergey,
Thanks to your example, creating a new style worked perfectly — no errors occurred at all.
I appreciate your help!

Kind regards, Tom
Post Reply