Hi I’ve tried following this tutorial UMG RichTextBlock hyperlink / href markup to add hyperlinks to a rich text box, but it doesn’t work as can be seen when I press play the links greyed out (screen shot below) and when I click on it, it doesn’t print the message.
Is this method not valid any more or am I doing something wrong? I would appreciate any help, code will be below.
Screen shot
.H
class FRichInlineHyperLinkDecorator : public FRichTextDecorator
{
public:
FRichInlineHyperLinkDecorator(URichTextBlock* InOwner, UHyperLinkRichTextDecorator* decorator);
virtual bool Supports(const FTextRunParseResults& RunParseResult, const FString& Text) const override;
protected:
virtual TSharedPtr<SWidget> CreateDecoratorWidget(const FTextRunInfo& RunInfo, const FTextBlockStyle& TextStyle) const override;
FHyperlinkStyle linkstyle;
FSimpleDelegate del;
mutable FString idString;
};
UCLASS()
class UNREALCOC_API UHyperLinkRichTextDecorator : public URichTextBlockDecorator
{
GENERATED_BODY()
public:
UHyperLinkRichTextDecorator(const FObjectInitializer& ObjectInitializer);
virtual TSharedPtr<ITextDecorator> CreateDecorator(URichTextBlock* InOwner) override;
UPROPERTY(EditAnywhere, Category = Appearance)
FHyperlinkStyle style;
UFUNCTION(BlueprintNativeEvent)
void ClickFun();
};
.cpp
//F
FRichInlineHyperLinkDecorator::FRichInlineHyperLinkDecorator(URichTextBlock* InOwner, UHyperLinkRichTextDecorator* decorator) : FRichTextDecorator(InOwner)
{
linkstyle = decorator->style;
del.BindLambda([=]() {
decorator->ClickFun();
});
}
bool FRichInlineHyperLinkDecorator::Supports(const FTextRunParseResults& RunParseResult, const FString& Text) const
{
if (RunParseResult.Name == TEXT("Link") && RunParseResult.MetaData.Contains(TEXT("id")))
{
const FTextRange& IdRange = RunParseResult.MetaData[TEXT("id")];
idString = Text.Mid(IdRange.BeginIndex, IdRange.EndIndex - IdRange.BeginIndex);
return true;
}
return false;
}
TSharedPtr<SWidget> FRichInlineHyperLinkDecorator::CreateDecoratorWidget(const FTextRunInfo& RunInfo, const FTextBlockStyle& TextStyle) const
{
const bool bWarnIfMissing = true;
TSharedPtr<FSlateHyperlinkRun::FWidgetViewModel> model = MakeShareable(new FSlateHyperlinkRun::FWidgetViewModel);
TSharedPtr<SRichTextHyperlink> link = SNew(SRichTextHyperlink, model.ToSharedRef())
.Text(FText::FromString("link"))
.Style(&linkstyle)
.OnNavigate(del);
return link;
}
//U
UHyperLinkRichTextDecorator::UHyperLinkRichTextDecorator(const FObjectInitializer& ObjectInitializer) : Super(ObjectInitializer)
{
}
TSharedPtr<ITextDecorator> UHyperLinkRichTextDecorator::CreateDecorator(URichTextBlock* InOwner)
{
return MakeShareable(new FRichInlineHyperLinkDecorator(InOwner, this));
}
void UHyperLinkRichTextDecorator::ClickFun_Implementation()
{
if (GEngine) {
GEngine->AddOnScreenDebugMessage(-1, 20, FColor::Red, "click link");
}
}