WPF中的RichTextBox中的上标+下划线内联
Superscript + Underline inline in a RichTextBox in WPF
我有一组文本想放入 RichTextBox 中,如下所示:
所以我使用了 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());
}
然而结果是这样的:
问题是,当将文本的基线更改为 superscript/subscript 时,下划线也会更改为该高度。我希望下划线保留在原处,并且超级脚本也能出现。
我只找到了一个不以编程方式完成的接近解决方案here。
由于这似乎是 RichTextBox 的局限性,最好的解决方案是 second answer of the question you linked, namely instead of using the normal letters, to use their Unicode superscript variants:
中提出的解决方案
"st" 变成“ˢᵗ”
"nd" 变成 "ⁿᵈ"
等等
您还应该删除基线设置:
//dateSuperscript.BaselineAlignment = BaselineAlignment.Superscript;
我尝试转换 link 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;
我有一组文本想放入 RichTextBox 中,如下所示:
所以我使用了 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());
}
然而结果是这样的:
问题是,当将文本的基线更改为 superscript/subscript 时,下划线也会更改为该高度。我希望下划线保留在原处,并且超级脚本也能出现。
我只找到了一个不以编程方式完成的接近解决方案here。
由于这似乎是 RichTextBox 的局限性,最好的解决方案是 second answer of the question you linked, namely instead of using the normal letters, to use their Unicode superscript variants:
中提出的解决方案"st" 变成“ˢᵗ”
"nd" 变成 "ⁿᵈ"
等等
您还应该删除基线设置:
//dateSuperscript.BaselineAlignment = BaselineAlignment.Superscript;
我尝试转换 link 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;