I am trying to make a multi-line scrollable text box that implements rich text. Although it does work for the most part, for some reason I can’t seem to get it to recognize any fonts besides the default font. For example:
<span font=“FREEDOM” color=“#0000ff” style=“Light” size=“54”>Test text</>
Will properly set the style, size, and color of the text but it will not set the font, the default font is used.
The following code was used to create the widget, it’s a modified version of some code I found in an old post<can’t recall authors name>; create a new widget blueprint then drag scroll box into the project followed by a Solanus Rich Text Block. Create the following event graph to add a decorator.
SolanusRichTextBlock.h
#pragma once
#include "CoreMinimal.h"
#include "Widget.h"
#include "Components/TextWidgetTypes.h"
#include "Runtime/Slate/Public/Widgets/Text/SRichTextBlock.h"
#include "RichTextBlockDecorator.h"
#include "Runtime/CoreUObject/Public/UObject/ConstructorHelpers.h"
#include "Runtime/Engine/Classes/Engine/Font.h"
#include "SolanusRichTextBlock.generated.h"
/**
*
*/
UCLASS()
class SOLANUS_API USolanusRichTextBlock : public UTextLayoutWidget
{
GENERATED_BODY()
public:
USolanusRichTextBlock(const FObjectInitializer& ObjectInitializer);
// UWidget interface
virtual void SynchronizeProperties() override;
// End of UWidget interface
// UVisual interface
virtual void ReleaseSlateResources(bool bReleaseChildren) override;
// End of UVisual interface
protected:
/** The text to display */
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = Content, meta = (MultiLine = "true"))
FText Text;
/** A bindable delegate to allow logic to drive the text of the widget */
UPROPERTY()
FGetText TextDelegate;
/** The default font for the text. */
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = Appearance)
TArray<FSlateFontInfo> Font;
/** The default color for the text. */
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = Appearance)
TArray<FLinearColor> Color;
/** Decorators */
UPROPERTY(EditAnywhere, Instanced, BlueprintReadWrite, Category = Decorators)
TArray<class URichTextBlockDecorator*> Decorators;
UFUNCTION(BlueprintCallable)
void addDecorator(FLinearColor _color, UFont * _font);
protected:
FTextBlockStyle DefaultStyle;
/** Native Slate Widget */
TSharedPtr<SRichTextBlock> MyRichTextBlock;
// UWidget interface
virtual TSharedRef<SWidget> RebuildWidget() override;
// End of UWidget interface
};
SolanusRichTextBlock.cpp
#include "SolanusRichTextBlock.h"
#include "UMGStyle.h"
USolanusRichTextBlock::USolanusRichTextBlock(const FObjectInitializer& ObjectInitializer)
: Super(ObjectInitializer) {
static ConstructorHelpers::FObjectFinder<UFont> RobotoFontObj(TEXT("/Engine/EngineFonts/Roboto"));
Font.Add(FSlateFontInfo(RobotoFontObj.Object, 12, FName("Regular")));
Color.Add(FLinearColor::White);
Decorators.Add(ObjectInitializer.CreateOptionalDefaultSubobject<URichTextBlockDecorator>(this, FName("DefaultDecorator")));
}
void USolanusRichTextBlock::addDecorator(FLinearColor _color, UFont * _font) {
URichTextBlockDecorator* dec = NewNamedObject<URichTextBlockDecorator>(this, FName(FName("Decorator"), Decorators.Num()));
dec->RevealedIndex = Decorators.Num();
Decorators.Add(dec);
Color.Add(_color);
Font.Add(FSlateFontInfo(_font, 12, FName("Default")));
}
void USolanusRichTextBlock::ReleaseSlateResources(bool bReleaseChildren) {
Super::ReleaseSlateResources(bReleaseChildren);
MyRichTextBlock.Reset();
}
TSharedRef<SWidget> USolanusRichTextBlock::RebuildWidget() {
DefaultStyle.SetFont(Font[0]);
DefaultStyle.SetColorAndOpacity(Color[0]);
TArray< TSharedRef< class ITextDecorator > > CreatedDecorators;
int i = 0;
for (URichTextBlockDecorator * Decorator: Decorators) {
CreatedDecorators.Add(Decorator->CreateDecorator(Font*, Color*));
i++;
}
MyRichTextBlock =
SNew(SRichTextBlock)
.TextStyle(&DefaultStyle)
.Decorators(CreatedDecorators);
return MyRichTextBlock.ToSharedRef();
}
void USolanusRichTextBlock::SynchronizeProperties() {
Super::SynchronizeProperties();
TAttribute<FText> TextBinding = OPTIONAL_BINDING(FText, Text);
MyRichTextBlock->SetText(TextBinding);
Super::SynchronizeTextLayoutProperties(*MyRichTextBlock);
}