-->

Superscript + Underline inline in a RichTextBox in

2019-08-07 13:18发布

问题:

I have a set of text that I'd like to put in a RichTextBox which goes like so:

So I used a RichTextBox since it allows me to do the following.

var zipCodeParagraph = new Paragraph();
string zipCodes = String.Empty;

var dateRun = new Underline(new Run(DateTime.Today.DayOfWeek + ", " + CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(DateTime.Today.Month) + ' ' + DateTime.Today.Day));
Underline dateSuperscript;

switch (DateTime.Today.Day % 10)
{
    case 1:
        dateSuperscript = new Underline(new Run("st"));
        break;
    case 2:
        dateSuperscript = new Underline(new Run("nd"));
        break;
    case 3:
        dateSuperscript = new Underline(new Run("rd"));
        break;
    default:
        dateSuperscript = new Underline(new Run("th"));
        break;
}

dateSuperscript.BaselineAlignment = BaselineAlignment.Superscript;

if (ZipCodes.Any())
{
    zipCodeParagraph.Inlines.Add(new Run("The following zip codes are facing a "));
    zipCodeParagraph.Inlines.Add(new Underline(new Run("Severe Weather Threat")));
    zipCodeParagraph.Inlines.Add(new Run(" on "));
    zipCodeParagraph.Inlines.Add(dateRun);
    zipCodeParagraph.Inlines.Add(dateSuperscript);
    zipCodes = String.Join(", ", ZipCodes.ToArray());
}

The outcome however is like so:

The problem is that when changing the baseline of a text to be superscript/subscript then the underline changes to that height as well. I'd like the underline to stay where it is and for the super-scripting to happen as well.

I have found only one close solution which does not do it programmatically here.

回答1:

I have tried to convert the same code which is mentioned in the link here. Refer the below code.

   FlowDocument mcFlowDoc = new FlowDocument();
        Hyperlink hyp = new Hyperlink();
        hyp.Foreground = Brushes.Black;
        TextBlock txt = new TextBlock();
        txt.Foreground = Brushes.Black;
        txt.Text = "Friday,April 10";           
        Run rn = new Run("th");
        rn.BaselineAlignment = BaselineAlignment.Superscript;
        txt.Inlines.Add(rn);
        hyp.Inlines.Add(txt);            
        Paragraph para = new Paragraph();
        para.Inlines.Add(new Run("The following zip codes are facing a "));
        para.Inlines.Add(new Underline(new Run("Severe Weather Threat")));
        para.Inlines.Add(new Run(" on "));
        para.Inlines.Add(hyp); 
        mcFlowDoc.Blocks.Add(para);
        RichTextBox mcRTB = new RichTextBox();
        mcRTB.Width = 560;
        mcRTB.Height = 100;
        mcRTB.Document = mcFlowDoc;


回答2:

As this seems a limitation of the RichTextBox, the best solution would be the one proposed in the second answer of the question you linked, namely instead of using the normal letters, to use their Unicode superscript variants:

"st" becomes "ˢᵗ"
"nd" becomes "ⁿᵈ"

etc.

You should also remove the baseline setting:

//dateSuperscript.BaselineAlignment = BaselineAlignment.Superscript;