【发布时间】:2020-10-08 19:42:59
【问题描述】:
我正在使用 Win2D 开发一个 UWP 应用程序,我想将某些文本放置在定义宽度和高度的 Rect(矩形)内。如果文本超过了 Rect 的宽度,那么我想根据 Rect 的宽度来剪辑文本。在这里,我想显示适合 Rect 的文本以及被剪裁的文本。如何使用 C# 在 Win2D 中的 CanvasTextLayout 中做到这一点?
CanvasDrawingSession drawingSession;
private void Draw(CanvasControl sender, CanvasDrawEventArgs args)
{
drawingSession = args.DrawingSession;
float xLoc = 100.0f;
float yLoc = 100.0f;
float CellWidth = 100.0f;
float CellHeight = 30.0f;
String fontFamily = "Tahoma";
int fontsize = 16;
FontStyle fontStyle = FontStyle.Normal;
FontWeight fontWeight = FontWeights.Normal;
string text = "abcdefghijklmnopqrstuvwxyz";
CanvasTextFormat format = GetTextFormat(fontFamily, fontsize, fontWeight, fontStyle);
CanvasTextLayout textLayout = new CanvasTextLayout(drawingSession, text, format, CellWidth, CellHeight);
textLayout.WordWrapping = CanvasWordWrapping.NoWrap;
textLayout.Options = CanvasDrawTextOptions.Clip;
drawingSession.DrawRectangle(xLoc, yLoc,CellWidth,CellHeight, Colors.Red, 1.0f);
drawingSession.DrawTextLayout(textLayout, xLoc, yLoc, Colors.Blue);
drawingSession.DrawRectangle(xLoc, yLoc + 100, CellWidth, CellHeight, Colors.Blue, 1.0f);
}
输出:
在这里,我在红色矩形内显示文本(以剪裁形式 - “abcdefghijklm”)。还想在蓝色矩形内显示剩余的剪辑文本(“nopqrstuvwxyz”)。该怎么做?
【问题讨论】: