Reputation: 21956
Here’s the code:
static readonly char[] s_expChar = "eE".ToCharArray();
static void setValue( TextBlock target, double val )
{
string text = val.ToString();
int indE = text.IndexOfAny( s_expChar );
if( indE > 0 )
{
string normal = text.Substring( 0, indE ) + "·10";
string ss = text.Substring( indE + 1 );
if( ss.StartsWith( "0" ) )
ss = ss.Substring( 1 );
else if( ss.StartsWith( "-0" ) )
ss = "-" + ss.Substring( 2 );
target.Inlines.Clear();
target.Inlines.Add( new Run( normal ) );
Run rSuper = new Run( ss );
Typography.SetVariants( rSuper, FontVariants.Superscript );
target.Inlines.Add( rSuper );
}
else
{
target.Text = text;
}
}
Here's the output:
As you see, vertical alignment of the -
character is broken, seems not affected by FontVariants.Superscript
. How to fix?
In debugger in live visual tree, I see 2 runs with the correct values, i.e. the second one has text -6
including the -
Upvotes: 1
Views: 807
Reputation: 5666
Typography.Variants
should be used with the right fonts and the right characters. So for example this piece of XAML can work not properly:
<TextBlock FontSize="20">
<TextBlock.Inlines>
<Run Text="2e" />
<Run Typography.Variants="Superscript" Text="-3" />
</TextBlock.Inlines>
</TextBlock>
As you noticed, the "-" symbol is not aligned as supposed. Indeed Typography.Variants
works just for an OpenType font (I suggest you Palatino Linotype or Segoe UI). Moreover is not correct to use the minus symbol with a Superscript
typography variant: the right character is called superscript minus (⁻
is its decimal representation).
So the correct XAML will be:
<TextBlock FontSize="20" FontFamily="Segoe UI">
<TextBlock.Inlines>
<Run Text="2e" />
<Run Typography.Variants="Superscript" Text="⁻3" />
</TextBlock.Inlines>
</TextBlock>
and it will be displayed as supposed. I hope it can help you.
Upvotes: 1