如何垂直对齐非等宽文本(How to vertically align non

编程入门 行业动态 更新时间:2024-10-12 05:53:11
如何垂直对齐非等宽文本(How to vertically align non-monospaced text)

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

测量将进入第一列的字符串 从所述第一列的最大宽度减去其宽度以获得差异 将其除以单个空间的宽度以获得所需的空间数 用必要数量的空格右键填充结果

这对我来说在逻辑上听起来很合理,但输出看起来像这样(使用可变宽度字体):

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

I'm trying to create two columns of left-aligned text with a variable-width font. My current process:

Measure the string that will be going into the first column Subtract its width from the maximum width of said first column to get the difference Divide that with the width of a single space to get the number of spaces needed Right-pad the result with the necessary number of spaces

This feels logically sound to me, but the output looks like this (with a variable-width font):

a 12345678910 as 12345678910 asd 12345678910 asdf 12345678910 asdfg 12345678910

The output I'm looking for is this:

a 12345678910 as 12345678910 asd 12345678910 asdf 12345678910 asdfg 12345678910

What am I missing?

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 }

Edit: Fixed dumb error with padding name instead of actually multiplying the amount of spaces. The result is a lot better now, but still a bit off. I do notice that the width of five spaces is not equal to the spaceWidth * 5 though...

最满意答案

在我编写此代码时,作者已添加了自己的解决方案。 然而,虽然他的代码似乎可以完成它的工作,但却增加了解决方案的复杂性。 我的代码有一个更简单的算法。

void addRows(string name, string value) { string teststring = name + ""; int spacesAdded = 0; while (maxNameWidth > graphics.MeasureString(teststring + "x", measurementFont).Width) { spacesAdded++; teststring += " "; } Console.WriteLine(":" + name + " ".PadRight(spacesAdded) + ":" + value + ":" + spacesAdded); }

我使用“x”获得与他相似的结果,它可以被其他字符替换,以测试更多的字符串,看它是否有效。 我测试了只有2个额外的字符串,其中包括非常宽的“ooooo”和非常窄的“.....”以获得令人满意的结果。


PS:我回答时似乎有过不同的想法。 由于我也在尝试学习新事物和寻找新方法,我试图得到这个问题的结果。 无论如何,这次我走在正确的道路上

Author has added his own solution while I was working on this code. Yet, though his code seems to do its work, it adds to the complexity of the solution. My code has a much simpler algorithm.

void addRows(string name, string value) { string teststring = name + ""; int spacesAdded = 0; while (maxNameWidth > graphics.MeasureString(teststring + "x", measurementFont).Width) { spacesAdded++; teststring += " "; } Console.WriteLine(":" + name + " ".PadRight(spacesAdded) + ":" + value + ":" + spacesAdded); }

I used the "x" to get a similar result as his and it can be replaced by other characters to test with more strings to see if it works. I have tested the with only 2 extra strings that include very wide "ooooo" and very narrow "....." to get a satisfying result.


PS: It seems I previously thought differently while answering. Since I am also trying to learn new things and to find new approaches, I tried to get a result for this question. Anyways, this time I am on the right path

更多推荐

本文发布于:2023-08-01 00:47:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1350657.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:文本   vertically   align

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!