Skip to content

Commit 103ec77

Browse files
Dhivya-SF4094PureWeen
authored andcommitted
Fixed Maui.Graphics GetStringSize Inverts Width and Height (#29574)
* Fixed incorrect width and height * Updated test sample * Updated method name * Update shared test sample * Given width and height request for graphicsview in sample * Added snapshots
1 parent 70d0742 commit 103ec77

File tree

7 files changed

+68
-0
lines changed

7 files changed

+68
-0
lines changed
10.9 KB
Loading
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
namespace Maui.Controls.Sample.Issues;
2+
3+
[Issue(IssueTracker.Github, 29562, "Maui.Graphics GetStringSize Inverts Width and Height", PlatformAffected.UWP)]
4+
public class Issue29562 : ContentPage
5+
{
6+
public Issue29562()
7+
{
8+
var stack = new StackLayout();
9+
var graphicsView = new GraphicsView
10+
{
11+
HeightRequest = 300,
12+
WidthRequest = 300,
13+
};
14+
15+
graphicsView.Drawable = new Issue29562_drawable();
16+
var label = new Label
17+
{
18+
Text = "Text should displayed in single line.",
19+
AutomationId = "Label"
20+
};
21+
22+
stack.Children.Add(label);
23+
stack.Children.Add(graphicsView);
24+
Content = stack;
25+
}
26+
}
27+
28+
class Issue29562_drawable : IDrawable
29+
{
30+
public void Draw(ICanvas canvas, RectF dirtyRect)
31+
{
32+
const string text = "Hello World";
33+
var fontSize = 20;
34+
var font = new Microsoft.Maui.Graphics.Font("Arial");
35+
36+
// Measure text
37+
var textSize = canvas.GetStringSize(text, font, fontSize);
38+
39+
float rectX = (dirtyRect.Width - textSize.Width) / 2;
40+
float rectY = (dirtyRect.Height - textSize.Height) / 2;
41+
42+
var boxRect = new RectF(rectX, rectY, textSize.Width, textSize.Height);
43+
canvas.DrawString(text, boxRect, HorizontalAlignment.Left, VerticalAlignment.Top);
44+
}
45+
}
5.43 KB
Loading
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
using NUnit.Framework;
2+
using UITest.Appium;
3+
using UITest.Core;
4+
5+
namespace Microsoft.Maui.TestCases.Tests.Issues;
6+
7+
public class Issue29562 : _IssuesUITest
8+
{
9+
public Issue29562(TestDevice testDevice) : base(testDevice)
10+
{
11+
}
12+
13+
public override string Issue => "Maui.Graphics GetStringSize Inverts Width and Height";
14+
15+
[Test]
16+
[Category(UITestCategories.GraphicsView)]
17+
public void GraphicsViewShouldNotWrapText()
18+
{
19+
App.WaitForElement("Label");
20+
VerifyScreenshot();
21+
}
22+
}
6.91 KB
Loading
13.8 KB
Loading

src/Graphics/src/Graphics/Platforms/Windows/PlatformStringSizeService.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ public SizeF GetStringSize(string value, IFont font, float textSize)
2424
public SizeF GetStringSize(string value, IFont font, float textSize, HorizontalAlignment horizontalAlignment, VerticalAlignment verticalAlignment)
2525
{
2626
var format = font.ToCanvasTextFormat(textSize);
27+
format.WordWrapping = CanvasWordWrapping.NoWrap;
2728

2829
var device = CanvasDevice.GetSharedDevice();
2930
var textLayout = new CanvasTextLayout(device, value, format, 0.0f, 0.0f);

0 commit comments

Comments
 (0)