【问题标题】:Display multiple lines of text in Oxyplot TextAnnotation在 Oxyplot TextAnnotation 中显示多行文本
【发布时间】:2016-05-07 20:29:33
【问题描述】:

我正在开发一个使用 xamarin 和 oxyplot 库的 Android 应用程序。我遇到了一个问题,我无法在 TextAnnotation 中添加多行文本。 我尝试了以下选项:

  var sb = new StringBuilder();
  sb.Append("Line1");
  sb.AppendLine(); // which is equal to Append(Environment.NewLine);
  sb.Append("\n");
  sb.Append("\r\n");
  sb.Append(System.Environment.NewLine);
  sb.Append("Line2");

并将其添加到 TextAnnotation 文本对象中:

        var textAnnotation1 = new TextAnnotation();
        textAnnotation1.Text = sb.ToString();
        textAnnotation1.Background = OxyColors.White;
        textAnnotation1.TextColor = OxyColors.Black;
        textAnnotation1.FontSize = 18;
        textAnnotation1.TextPosition = new DataPoint(4,_vericalLineYaxis);
        plotModel.Annotations.Add(textAnnotation1);

但一切都好用。

我的目标是让文本看起来像这样:

第 1 行
2号线

目前显示为:

1号线2号线

任何帮助将不胜感激。

【问题讨论】:

    标签: android xamarin oxyplot


    【解决方案1】:

    Android 平台目前支持多行注释。

    OxyPlot 正在调用 Android.Canvas.DrawText 并且该函数不支持文本换行,它是一个相当低级的原始绘图例程。

    如果您想修改源代码,可以通过使用静态布局而不是当前的canvas.DrawText 来完成。

    这样的事情会让你开始(但未经测试):

    public void DrawText (string text, float x, float y, Paint paint) 
    {
        TextPaint textPaint = new TextPaint();
        StaticLayout textLayout = new StaticLayout(text, textPaint, canvas.getWidth(), Alignment.ALIGN_NORMAL, 1.0f, 0.0f, False);
        canvas.Save();
        canvas.Translate(x, y);
        textLayout.Draw(canvas);
        canvas.Restore();
    }
    

    仅供参考:Oxyplot 的 SVG 渲染器通过在 "\r\n" 上拆分字符串并为每行渲染一个单独的元素来手动处理多行文本,因此可以对 Android 执行相同的操作使用StaticLayout (性能较慢,但易于修改/测试):

    var lines = Regex.Split(text, "\r\n");
    if (valign == VerticalAlignment.Bottom)
    {
        for (var i = lines.Length - 1; i >= 0; i--)
        {
            var line = lines[i];
            var size = this.MeasureText(line, fontFamily, fontSize, fontWeight);
            this.w.WriteText(p, line, c, fontFamily, fontSize, fontWeight, rotate, halign, valign);
    
            p += new ScreenVector(Math.Sin(rotate / 180.0 * Math.PI) * size.Height, Math.Cos(rotate / 180.0 * Math.PI) * size.Height);
        }
    }
    

    【讨论】:

    • SushiHangover,我知道你想帮忙,但我不知道你刚才说了什么。我觉得你好像回答了一个不同的问题。我知道多行注释还没有实现。老实说,感谢您对此进行调查。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-06
    • 1970-01-01
    相关资源
    最近更新 更多