我正在处理一个项目,需要我近似于以图像形式呈现的文本以及用于文本的DHTML编辑器。这些图像是使用.NET 4 DrawingContext对象的DrawText方法呈现的。
DrawText方法将文本以及字体信息和尺寸一起输入,并计算必要的换行,以使文本尽可能适应空间,如果文本太长,则在末尾放置省略号。因此,如果我有以下代码来绘制矩形中的文本,它会进行缩写:
有没有一种方法可以计算文本如何被换行?在这个例子中,输出的文本会在“2升”和“48英里每加仑”处换行,如下图所示:
DrawText方法将文本以及字体信息和尺寸一起输入,并计算必要的换行,以使文本尽可能适应空间,如果文本太长,则在末尾放置省略号。因此,如果我有以下代码来绘制矩形中的文本,它会进行缩写:
string longText = @"A choice of five engines, although the 2-liter turbo diesel, supposedly good for 48 m.p.g. highway, is not coming to America, at least for now. A 300-horsepower supercharged gasoline engine will likely be the first offered in the United States. All models will use start-stop technology, and fuel consumption will decrease by an average of 19 percent across the A6 lineup. A 245-horsepower A6 hybrid was also unveiled, but no decision has yet been made as to its North America sales prospects. Figure later in 2012, if sufficient demand is detected.";
var drawing = new DrawingGroup();
using (var context = drawing.Open())
{
var text = new FormattedText(longText,
CultureInfo.CurrentCulture,
FlowDirection.LeftToRight,
new Typeface("Calibri"),
30,
Brushes.Green);
text.MaxTextHeight = myRect.Height;
text.MaxTextWidth = myRect.Width;
context.DrawText(text, new Point(0, 0));
}
var db = new DrawingBrush(drawing);
db.Stretch = Stretch.None;
myRect.Fill = db;
有没有一种方法可以计算文本如何被换行?在这个例子中,输出的文本会在“2升”和“48英里每加仑”处换行,如下图所示: