我正试图把一个字串成多行。每一行都有一个定义的宽度。
例如,如果我将word包装到宽度为120像素的区域,就会得到这个结果:
洛雷姆和多洛坐在一起, 圣洁的小精灵。Sed自卫队 天鹅绒的,非秃鹫一样坐着, [医]玻璃体泪液。在简历中 朱斯托,塞姆。多涅茨 髓核,nisi nec矢状突的结果, 扫描电子显微镜( sem ) 在nec nec neque。皮伦提斯克 居住者morbi三体老人等 [医]美人鱼 埃斯达斯。Etiam erat est,pellentesque 一开始有点小问题,有前科的。 卵裂无毛。脯氨酸 奈克人。[医]金红花 萨皮恩,尤特里斯·埃拉特拍卖公司。 [医]尺三叉神经损伤,矢状体炎 毛里香三味 使人感到不快。 [医]长曲霉。明渠 罗汉果发酵菌明渠 [医]前庭妊娠 [医][医]准大调味品基斯克 [医]非变种 自由龙虾欧盟。前庭欧盟 塔皮斯·马萨,我叫奥西。 [医]库拉比图尔 绝热设施。毛里士维尔 乌贼。脯氨酰亚胺nec 扫描电镜扫描电镜下前庭部玻璃体。
发布于 2010-10-18 17:21:07
static void Main(string[] args)
{
List<string> lines = WrapText("Add some text", 300, "Calibri", 11);
foreach (var item in lines)
{
Console.WriteLine(item);
}
Console.ReadLine();
}
static List<string> WrapText(string text, double pixels, string fontFamily,
float emSize)
{
string[] originalLines = text.Split(new string[] { " " },
StringSplitOptions.None);
List<string> wrappedLines = new List<string>();
StringBuilder actualLine = new StringBuilder();
double actualWidth = 0;
foreach (var item in originalLines)
{
FormattedText formatted = new FormattedText(item,
CultureInfo.CurrentCulture,
System.Windows.FlowDirection.LeftToRight,
new Typeface(fontFamily), emSize, Brushes.Black);
actualLine.Append(item + " ");
actualWidth += formatted.Width;
if (actualWidth > pixels)
{
wrappedLines.Add(actualLine.ToString());
actualLine.Clear();
actualWidth = 0;
}
}
if(actualLine.Length > 0)
wrappedLines.Add(actualLine.ToString());
return wrappedLines;
}添加WindowsBase和PresentationCore库。
发布于 2013-10-11 01:29:14
这是我为我的XNA游戏设计的一个版本.
(请注意,这是一个片段,而不是一个正确的类定义。享受吧!)
using System;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
public static float StringWidth(SpriteFont font, string text)
{
return font.MeasureString(text).X;
}
public static string WrapText(SpriteFont font, string text, float lineWidth)
{
const string space = " ";
string[] words = text.Split(new string[] { space }, StringSplitOptions.None);
float spaceWidth = StringWidth(font, space),
spaceLeft = lineWidth,
wordWidth;
StringBuilder result = new StringBuilder();
foreach (string word in words)
{
wordWidth = StringWidth(font, word);
if (wordWidth + spaceWidth > spaceLeft)
{
result.AppendLine();
spaceLeft = lineWidth - wordWidth;
}
else
{
spaceLeft -= (wordWidth + spaceWidth);
}
result.Append(word + space);
}
return result.ToString();
}发布于 2016-04-22 21:21:18
谢谢!对于在Windows中使用它,我采用了作为-cii的回答中的一种方法,并进行了一些更改。我使用的是TextRenderer.MeasureText而不是FormattedText
static List<string> WrapText(string text, double pixels, Font font)
{
string[] originalLines = text.Split(new string[] { " " },
StringSplitOptions.None);
List<string> wrappedLines = new List<string>();
StringBuilder actualLine = new StringBuilder();
double actualWidth = 0;
foreach (var item in originalLines)
{
int w = TextRenderer.MeasureText(item + " ", font).Width;
actualWidth += w;
if (actualWidth > pixels)
{
wrappedLines.Add(actualLine.ToString());
actualLine.Clear();
actualWidth = w;
}
actualLine.Append(item + " ");
}
if(actualLine.Length > 0)
wrappedLines.Add(actualLine.ToString());
return wrappedLines;
}请注意:行actualLine.Append(item +“");需要在检查宽度后放置,因为如果actualWidth >像素,则该单词必须位于下一行。
https://stackoverflow.com/questions/3961278
复制相似问题