在WPF中的RichTextBox中内联上划线+下划线

时间:2015-05-04 20:05:01

标签: c# wpf richtextbox superscript

我有一组文本,我想把它放在RichTextBox中,如下所示:

Result I want to see

所以我使用了RichTextBox,因为它允许我执行以下操作。

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());
}

结果却如此:

Result I am getting

问题在于,当将文本的基线更改为上标/下标时,下划线也会更改为该高度。我希望下划线能够保持原样并且超级脚本也可以发生。

我发现只有一个密切的解决方案没有以编程方式here进行。

2 个答案:

答案 0 :(得分:1)

由于这似乎是RichTextBox的限制,因此最好的解决方案是在您链接的问题的second answer中提出的解决方案,即使用正常字母,而不是使用他们的Unicode superscript variants

“st”变为“ˢᵗ”
“nd”变为“ⁿ​​ᵈ”

您还应该删除基线设置:

//dateSuperscript.BaselineAlignment = BaselineAlignment.Superscript;

答案 1 :(得分:1)

我尝试转换链接here中提到的相同代码。 请参阅以下代码。

   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;