我正在嘗試構建一個 WPF 應用程式來協助進行一些數學修訂,但在將索引值顯示為上標時遇到了問題。我正在嘗試將生成的問題字串格式化為代碼隱藏的所需輸出。我最初試圖將生成的問題簡單地顯示為文本塊中的字串。(在輸入字串中每個值的開頭和結尾用“#”標記索引值。)
private char ConvertSingleToSuperscript(char input)
{
switch (input)
{
case '0':
return '?';
case '1':
return '1';
case '2':
return '2';
case '3':
return '3';
case '4':
return '?';
case '5':
return '?';
case '6':
return '6';
case '7':
return '?';
case '8':
return '?';
case '9':
return '?';
case ' ':
return '?';
case '-':
return '?';
case '=':
return '?';
case '/':
return char.Parse(char.ConvertFromUtf32(0x2044));
default:
throw new Exception("Incorrect character entered as superscript, change entered indice value.");
}
}
public string ProcessSuperscript(string input)
{
char[] tempArray = input.ToCharArray();
string outputString = "";
bool processingIndice = false;
foreach(char c in tempArray)
{
if(c == '#')
{
processingIndice = !processingIndice;
}
if (processingIndice)
{
outputString = ConvertSingleToSuperscript(c);
}
else
{
outputString = c;
}
}
return outputString;
}
使用這個,或者讓我的 'ConvertSingleToSuperScript' 方法回傳通過相關上標 unicode 生成的字符值將始終回傳字串,其中 1、2 和 3 顯示為上標專案,其余部分將回傳與其余部分相同大小的文本字串中的“非索引”值。
最近,我嘗試使用 textblock 行內運行來實作相同的結果:
private void FormatQuestionSuperScriptDisplay(TextBlock displayBlock, string variableText)
{
char[] tempArray = variableText.ToCharArray();
string processString = "";
bool processingIndice = false;
foreach (char c in tempArray)
{
if (c == '#')
{
if (!processingIndice)
{
displayBlock.Inlines.Add(processString);
Console.WriteLine(processString " as normal");
}
else
{
displayBlock.Inlines.Add(new Run(processString) { BaselineAlignment = BaselineAlignment.Superscript });
Console.WriteLine(processString " as indice.");
}
processString = "";
processingIndice = !processingIndice;
continue;
}
processString = c;
}
displayBlock.Inlines.Add(processString);
}
}
我一定是在某個地方搞砸了,現在我的輸出會從字串中出來,如下圖所示:“abcdefghijklmnopqrstuvwxyz 12#32# 15#4589# test”
對于那些無法看到影像的人,“i”和“n”的字符顯示為上標字符
One more question I have is that all the numerical values once ran through the Inlines.Add() method are processed as superscript characters as well, how can process them (I'm not sure what the correct term is so I'll say) at a normal size matching the rest of the string?
Any assistance or pointers on what I should be reading up on would be mega helpful. Thanks for your time.
uj5u.com熱心網友回復:
這對我有用。檢查函式 ProcessSuperscript。
背后的代碼
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
namespace SuperScript
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
FormatQuestionSuperScriptDisplay(tbText, "abcdefghijklmnopqrstuvwxyz 12#32# 15#4589# test");
}
private char ConvertSingleToSuperscript(char input)
{
switch (input)
{
case '0':
return '?';
case '1':
return '1';
case '2':
return '2';
case '3':
return '3';
case '4':
return '?';
case '5':
return '?';
case '6':
return '6';
case '7':
return '?';
case '8':
return '?';
case '9':
return '?';
case ' ':
return '?';
case '-':
return '?';
case '=':
return '?';
case '/':
return char.Parse(char.ConvertFromUtf32(0x2044));
default:
throw new Exception("Incorrect character entered as superscript, change entered indice value.");
}
}
public string ProcessSuperscript(string input)
{
char[] tempArray = input.ToCharArray();
string outputString = "";
bool processingIndice = false;
foreach (char c in tempArray)
{
if (c == '#')
{
processingIndice = !processingIndice;
continue; //<- This code added.
}
if (processingIndice)
{
outputString = ConvertSingleToSuperscript(c);
}
else
{
outputString = c;
}
}
return outputString;
}
private void FormatQuestionSuperScriptDisplay(TextBlock displayBlock, string variableText)
{
char[] tempArray = variableText.ToCharArray();
string processString = "";
bool processingIndice = false;
foreach (char c in tempArray)
{
if (c == '#')
{
if (!processingIndice)
{
displayBlock.Inlines.Add(processString);
Console.WriteLine(processString " as normal");
}
else
{
displayBlock.Inlines.Add(new Run(processString) { BaselineAlignment = BaselineAlignment.Superscript });
Console.WriteLine(processString " as indice.");
}
processString = "";
processingIndice = !processingIndice;
continue;
}
processString = c;
}
displayBlock.Inlines.Add(processString);
}
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/376953.html
標籤:c# wpf indices typography
