WPF 中 RichTextBox 中的上标 + 下划线内联 [英] Superscript + Underline inline in a RichTextBox in WPF

查看:67
本文介绍了WPF 中 RichTextBox 中的上标 + 下划线内联的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一组文本要放入 RichTextBox 中,如下所示:

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

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

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 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.

推荐答案

我已尝试转换链接 此处.参考下面的代码.

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;

这篇关于WPF 中 RichTextBox 中的上标 + 下划线内联的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆