gpt4 book ai didi

c# - 如何垂直对齐非等宽文本

转载 作者:行者123 更新时间:2023-11-30 15:54:38 25 4
gpt4 key购买 nike

我正在尝试使用可变宽度字体创建两列左对齐文本。我目前的流程:

  1. 测量将进入第一列的字符串
  2. 用第一列的最大宽度减去它的宽度得到差值
  3. 将其除以单个空格的宽度以获得所需的空格数
  4. 用必要数量的空格向右填充结果

我觉得这在逻辑上是合理的,但输出看起来像这样(使用可变宽度字体):

a                    12345678910
as 12345678910
asd 12345678910
asdf 12345678910
asdfg 12345678910

我正在寻找的输出是这样的:

a          12345678910
as 12345678910
asd 12345678910
asdf 12345678910
asdfg 12345678910

我错过了什么?

const int maxNameWidth = 150;

Font measurementFont;
Graphics graphics;
float spaceWidth;

void Main()
{
measurementFont = new Font("Arial", 14);
graphics = Graphics.FromImage(new Bitmap(1, 1));
spaceWidth = graphics.MeasureString(" ", measurementFont).Width;

addRow("a", "12345678910");
addRow("as", "12345678910");
addRow("asd", "12345678910");
addRow("asdf", "12345678910");
addRow("asdfg", "12345678910");

measurementFont.Dispose();
graphics.Dispose();
}

void addRow(string name, string value) {
float width = graphics.MeasureString(name, measurementFont).Width;
int amountOfSpacesNeeded = Convert.ToInt32((maxNameWidth - width) / spaceWidth);
Console.WriteLine(name + " ".PadRight(amountOfSpacesNeeded) + content); // The console font is Arial
}

编辑:修复了填充 name 而不是实际乘以空格量的愚蠢错误。结果现在好多了,但还是有点偏差。我确实注意到五个空格的宽度不等于 spaceWidth * 5 但是...

最佳答案

终于明白了。但是我认为这个解决方案是令人作呕的垃圾,所以我会保持这个问题开放,直到有人有更好/更少的蛮力方法。

基本上,由于无法计算的字距调整(据我所知),计算让我少了 1-2 个空格,我只是再次测量字符串并从中添加或减去空格,直到它到达 maxNameWidth 的另一侧它的方向;例如。如果太短则添加一个空格,再次测量并查看是否超过 maxNameWidth ,如果是则停止,反之亦然。

完整代码:

const int maxNameWidth = 150;

Font measurementFont;
Graphics graphics;
float spaceWidth, xWidth;

enum Direction { None, FromHigh, FromLow };

void Main()
{
measurementFont = new Font("Arial", 14);
graphics = Graphics.FromImage(new Bitmap(1, 1));
spaceWidth = graphics.MeasureString(" ", measurementFont).Width;
xWidth = graphics.MeasureString("x", measurementFont).Width;

addRow("a", "12345678910");
addRow("as", "12345678910");
addRow("asd", "12345678910");
addRow("asdf", "12345678910");
addRow("asdfg", "12345678910");

measurementFont.Dispose();
graphics.Dispose();
}

void addRow(string name, string value) {

float width = graphics.MeasureString(name, measurementFont).Width;
int amountOfSpacesNeeded = Convert.ToInt32((maxNameWidth - width) / spaceWidth);

string firstColumn = name + " ".PadRight(amountOfSpacesNeeded);
float currWidth;
Direction dir = Direction.None;
while (true) {
// I add an 'x' here because just measuring a bunch of spaces does not work (the width of one space is apparently equal to the width of five according to graphics)
currWidth = graphics.MeasureString(firstColumn + "x", measurementFont).Width - xWidth;
if (((dir == Direction.FromLow) || (dir == Direction.None)) && (currWidth < maxNameWidth)) {
dir = Direction.FromLow;
firstColumn += " ";
}
else if (((dir == Direction.FromHigh) || (dir == Direction.None)) && (currWidth > maxNameWidth)) {
dir = Direction.FromHigh;
firstColumn = firstColumn.Remove(firstColumn.Length - 1);
}
else {
break;
}
}

Console.WriteLine(firstColumn + value);

}

输出(用 14pt Arial 粘贴到某处以查看对齐方式):

a                     12345678910
as 12345678910
asd 12345678910
asdf 12345678910
asdfg 12345678910

关于c# - 如何垂直对齐非等宽文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50013521/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com