【问题标题】:How to get the remaining text after clipping in CanvasTextLayout in Win2D using C#?如何使用 C# 在 Win2D 中的 CanvasTextLayout 中剪辑后获取剩余的文本?
【发布时间】: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”)。该怎么做?

【问题讨论】:

    标签: c# uwp win2d


    【解决方案1】:

    Win2D没有提供一定的API直接获取截断的文本,但是我有个想法,就是通过计算文本的宽度来判断当前输入的文本被截断的位置。

    private void Draw(CanvasControl sender, CanvasDrawEventArgs args)
    {
        ...
        if (textLayout.LayoutBounds.Width > CellWidth)
        {
            int length = text.Length;
            int index = 0;
            for (; index < length; index++)
            {
                var regions = textLayout.GetCharacterRegions(0, index);
                if (regions.Length > 0)
                {
                    var region = regions.First();
                    if (region.LayoutBounds.Width > CellWidth)
                        break;
                }
            }
            string trimmed = text.Substring(index - 1);
    
            var textLayout2 = new CanvasTextLayout(drawingSession, trimmed, format, CellWidth, CellHeight);
            textLayout2.WordWrapping = CanvasWordWrapping.NoWrap;
            textLayout2.Options = CanvasDrawTextOptions.Clip;
            drawingSession.DrawTextLayout(textLayout2, xLoc, yLoc + 100, Colors.Red);
        }
    }
    

    我们可以通过textLayout.GetCharacterRegions()获取指定区域的文字宽度,并与预设宽度进行比较,得到渲染文字溢出的位置。

    但是在渲染文本的时候,有些字符可能渲染不完整,所以在获取溢出文本的时候,我又多取了一个字符。

    【讨论】:

    • 非常感谢您的解决方案。正如您所说,在渲染文本时,一些未完全截断的字符会再次使用该字符进行渲染。如何解决?请需要你的帮助。
    • 我们可以根据目前的答案稍微修改一下。可以看到,通过判断指定长度的字符串的宽度,我们可以知道字符串在哪里被截断了,那么我们可以将输入的字符串一分为二,textLayout渲染前半部分,textLayout2渲染后半部分一半,而不是 Now textLayout 呈现完整的字符串
    猜你喜欢
    • 1970-01-01
    • 2019-01-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-19
    • 2011-04-16
    • 2013-02-07
    相关资源
    最近更新 更多