Skip to content

Fixed Maui.Graphics GetStringSize Inverts Width and Height #29574

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
45 changes: 45 additions & 0 deletions src/Controls/tests/TestCases.HostApp/Issues/Issue29562.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
namespace Maui.Controls.Sample.Issues;

[Issue(IssueTracker.Github, 29562, "Maui.Graphics GetStringSize Inverts Width and Height", PlatformAffected.UWP)]
public class Issue29562 : ContentPage
{
public Issue29562()
{
var stack = new StackLayout();
var graphicsView = new GraphicsView
{
HeightRequest = 300,
WidthRequest = 300,
};

graphicsView.Drawable = new Issue29562_drawable();
var label = new Label
{
Text = "Text should displayed in single line.",
AutomationId = "Label"
};

stack.Children.Add(label);
stack.Children.Add(graphicsView);
Content = stack;
}
}

class Issue29562_drawable : IDrawable
{
public void Draw(ICanvas canvas, RectF dirtyRect)
{
const string text = "Hello World";
var fontSize = 20;
var font = new Microsoft.Maui.Graphics.Font("Arial");

// Measure text
var textSize = canvas.GetStringSize(text, font, fontSize);

float rectX = (dirtyRect.Width - textSize.Width) / 2;
float rectY = (dirtyRect.Height - textSize.Height) / 2;

var boxRect = new RectF(rectX, rectY, textSize.Width, textSize.Height);
canvas.DrawString(text, boxRect, HorizontalAlignment.Left, VerticalAlignment.Top);
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using NUnit.Framework;
using UITest.Appium;
using UITest.Core;

namespace Microsoft.Maui.TestCases.Tests.Issues;

public class Issue29562 : _IssuesUITest
{
public Issue29562(TestDevice testDevice) : base(testDevice)
{
}

public override string Issue => "Maui.Graphics GetStringSize Inverts Width and Height";

[Test]
[Category(UITestCategories.GraphicsView)]
public void GraphicsViewShouldNotWrapText()
{
App.WaitForElement("Label");
VerifyScreenshot();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pending snapshots. Running a build.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jsuarezruiz Added the Snapshots.

}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public SizeF GetStringSize(string value, IFont font, float textSize)
public SizeF GetStringSize(string value, IFont font, float textSize, HorizontalAlignment horizontalAlignment, VerticalAlignment verticalAlignment)
{
var format = font.ToCanvasTextFormat(textSize);
format.WordWrapping = CanvasWordWrapping.NoWrap;

var device = CanvasDevice.GetSharedDevice();
var textLayout = new CanvasTextLayout(device, value, format, 0.0f, 0.0f);
Expand Down
Loading