简体   繁体   中英

Superscript + Underline inline in a RichTextBox in WPF

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 .

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;

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM