Joy Rex
Joy Rex

Reputation: 618

How to format superscript string in C#?

I need to format some string in Superscript (code behind) in c#,

for example,

Input => 100 Output => 10^2

where Output<string> is a Clr Property

I have tried with the below code snippet,

 TextBlock _textBlock = new TextBlock();
    _textBlock.Text = "10";
    _textBlock.Inlines.Add(new Run() { BaselineAlignment  = BaselineAlignment.Superscript, Text = "2"});

As Typography is Read Only property in the above case, i can't set the Typography.Variants in code behind.

Note: Please do note that this question is not about rendering, this is about reading the Text as string. Expected value is 10^2. While suggesting duplicates please check other question addresses this.

Could any one guide me on this?

And also while set BaselineAlignment.Superscript like the above mentioned code snippet, it doesn't render like the superscript (small font) instead it renders in the same FontSize (instead it looks like setting the Margin property for the superscript content alone like new Thickness(0,0,0,FontSize) )

Upvotes: 3

Views: 7506

Answers (1)

Steve
Steve

Reputation: 3061

You need to use the Unicode character for superscript 2. Have a look here: http://msdn.microsoft.com/en-us/library/aa664669%28v=vs.71%29.aspx

If you want it in a string literal you could use:

 var value = "10\xB2";

Live Demo

In WPF you can use Typeography variants.

Set superscript and subscript in formatted text in wpf

Upvotes: 3

Related Questions