AB4D Forum

Full Version: Multiline centered text using TextBlockVisual3D?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I am using TextBlockVisual3D to display multline text (each line separated by \r\n).  I would like each line of text to be centered.  I've tried a few things, but nothing gave me the result that I am interested in.  What is the best way to display three lines of text with each line centered?  See the attached image for example.  I want HDPE, 19.87', and 134,840 lbs to be centered on that yellow cylinder.

Thanks,
Arthur
This is an interesting question.

With a standard TextBlock it is possible to achieve this with setting the TextAlignment to Center.

Currently the TextBlockVisual3D does not support TextAlignment. But because TextBlockVisual3D is highly customizable, you can set that property to the underlying TextBlock with using a derived class:

Code:
   public class CenteredTextBlockVisual3D : TextBlockVisual3D
   {
       // To add some customization to how TextBlock is rendered,
       // you can override the UpdateTextBlock
       protected override void UpdateTextBlock()
       {
           base.UpdateTextBlock();

           textBlock.TextAlignment = TextAlignment.Center;
       }
   }

Then just use the CenteredTextBlockVisual3D instead of the standard TextBlockVisual3D.

In similar way it is also possible to customize the Border and Plane's MeshGeometry3D - see the CustomTextBlockVisual3DSample for more info.


I see that the TextAlignment can be useful for other users also. Therefore I have added it to the TextBlockVisual3D so it will be available in the next version of Ab3d.PowerToys library.
Thank you, that works well!