locked
TextBlock (RichTextBlock) with Superscript and Subscript parts inside the Text (Programmatically)

    Question

  • Hello,

    how to set programmatically the text of a TextBlock (or a RichTextBlock) with Superscript and Subscript parts inside the text?

    E.g. the text can be "Today in the 2^{nd} of December I set x_{2} boxes".

    where ^{nd} means nd is superscript,

    and where _{2} means 2 is subscript.

    Ideally i'd like to start from the string s = "Today in the 2nd of December I set x2 boxes", and with string find to replace part of it with superscript and subscript parts.

    Thanks

    Wednesday, December 10, 2014 3:29 PM

Answers

  • Hi pf16mirage,

    I think this could be what you need:

    Typography.Variants attached property is the one you need to use, however if you have the text like Today in the 2^{nd} of December I set x_{2} boxes, you may need translate them by your self into different run controls and display on the RichTextBlock.

    For instance you filter the ^ mark in your string and put the "nd" inside the braces into a Run Control which Typography.Variants is Superscript. The same for Subscript.

    --James


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Thursday, December 11, 2014 3:11 AM
    Moderator
  • Yes, of course its possible, see my codes:

        <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
            <RichTextBlock x:Name="t" FontSize="50" Margin="360,267,0,0"></RichTextBlock>
        </Grid>


                Paragraph para = new Paragraph();
    
                Run subscript = new Run() { Text = "2" };
                Typography.SetVariants(subscript, FontVariants.Subscript);
                Run subscript1 = new Run() { Text = "4" };
                Typography.SetVariants(subscript1, FontVariants.Subscript);
    
                Run normal = new Run() { Text = "H" };
                Run normal1 = new Run() { Text = "O" };
    
                para.Inlines.Add(normal);
                para.Inlines.Add(subscript);
                para.Inlines.Add(normal1);
                para.Inlines.Add(subscript1);
    
                t.Blocks.Add(para);

    The result:

    --James


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, December 15, 2014 7:31 AM
    Moderator

All replies

  • Hi pf16mirage,

    I think this could be what you need:

    Typography.Variants attached property is the one you need to use, however if you have the text like Today in the 2^{nd} of December I set x_{2} boxes, you may need translate them by your self into different run controls and display on the RichTextBlock.

    For instance you filter the ^ mark in your string and put the "nd" inside the braces into a Run Control which Typography.Variants is Superscript. The same for Subscript.

    --James


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Thursday, December 11, 2014 3:11 AM
    Moderator
  • Hi Jamles and thanks for the response. 

    I know that using xaml, but I want to be able to do this in runtime, i.e. programmatically, without using some somepage.xaml for that.

    Is this possible?

    Thanks!

    P.S. I put the braces just for illustration. I don't have braces and ^ or _ in my string. I'll just do a find and replace.
    • Edited by pf16mirage Thursday, December 11, 2014 5:24 PM
    Thursday, December 11, 2014 5:23 PM
  • Yes, of course its possible, see my codes:

        <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
            <RichTextBlock x:Name="t" FontSize="50" Margin="360,267,0,0"></RichTextBlock>
        </Grid>


                Paragraph para = new Paragraph();
    
                Run subscript = new Run() { Text = "2" };
                Typography.SetVariants(subscript, FontVariants.Subscript);
                Run subscript1 = new Run() { Text = "4" };
                Typography.SetVariants(subscript1, FontVariants.Subscript);
    
                Run normal = new Run() { Text = "H" };
                Run normal1 = new Run() { Text = "O" };
    
                para.Inlines.Add(normal);
                para.Inlines.Add(subscript);
                para.Inlines.Add(normal1);
                para.Inlines.Add(subscript1);
    
                t.Blocks.Add(para);

    The result:

    --James


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, December 15, 2014 7:31 AM
    Moderator