Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Aug 18, 2025

Note

Are you waiting for the changes in this PR to be merged?
It would be very helpful if you could test the resulting artifacts from this PR and let us know in a comment if this change resolves your issue. Thank you!

Description

This PR fixes an issue on Windows where setting LineHeight to values less than 1 had no visual effect on Labels. The problem was that WinUI TextBlock's default LineStackingStrategy is MaxHeight, which ignores LineHeight values that are smaller than the natural line height.

Problem

When using LineHeight multipliers < 1 on Windows Labels, the line spacing would not decrease as expected:

<Label Text="Test text with tight line spacing" 
       LineHeight="0.8" 
       FontSize="20" />

Before fix: LineHeight="0.8" had no visual effect - lines remained at default spacing
After fix: LineHeight="0.8" properly reduces line spacing

Root Cause

The WinUI TextBlock control has two relevant properties:

  • LineHeight: Sets the desired line height
  • LineStackingStrategy: Determines how the LineHeight value is applied

The default LineStackingStrategy.MaxHeight uses the larger of:

  • The natural height needed for the text
  • The specified LineHeight value

This means LineHeight values < 1 are effectively ignored since the natural height is typically larger.

Solution

Modified the UpdateLineHeight method in TextBlockExtensions.cs to set LineStackingStrategy to BlockLineHeight when a LineHeight is specified. BlockLineHeight always uses the exact LineHeight value, allowing values < 1 to take effect.

public static void UpdateLineHeight(this TextBlock platformControl, ILabel label)
{
    if (label.LineHeight >= 0)
    {
        platformControl.LineHeight = label.LineHeight * platformControl.FontSize;
        platformControl.LineStackingStrategy = LineStackingStrategy.BlockLineHeight; // NEW
    }
}

Additional Changes

  • Enabled LineHeight tests for Windows platform (removed #if !WINDOWS conditionals)
  • Added GetNativeLineHeight() and GetNativeCharacterSpacing() methods for Windows test infrastructure
  • Added specific tests to verify LineStackingStrategy behavior

Testing

The fix ensures consistent LineHeight behavior across all platforms:

  • LineHeight < 1: Now properly reduces line spacing on Windows
  • LineHeight > 1: Continues to work as before
  • LineHeight = -1 (default): Uses default behavior (MaxHeight strategy)
  • Cross-platform consistency: Windows now matches iOS and Android behavior

Fixes #24520.


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copilot AI changed the title [WIP] [Windows] Change the LineStackingStrategy to BlockLineHeight for Labels on Windows [Windows] Fix LineHeight values < 1 having no effect by setting LineStackingStrategy to BlockLineHeight Aug 18, 2025
Copilot AI requested a review from PureWeen August 18, 2025 20:18
Copilot finished work on behalf of PureWeen August 18, 2025 20:18
Copilot AI and others added 3 commits August 18, 2025 15:37
Co-authored-by: PureWeen <5375137+PureWeen@users.noreply.github.com>
Co-authored-by: PureWeen <5375137+PureWeen@users.noreply.github.com>
@PureWeen PureWeen changed the base branch from main to net10.0 August 18, 2025 20:38
if (label.LineHeight >= 0)
{
platformControl.LineHeight = label.LineHeight * platformControl.FontSize;
platformControl.LineStackingStrategy = LineStackingStrategy.BlockLineHeight;
Copy link
Contributor

Choose a reason for hiding this comment

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

I wonder if it would be better to set some default style here: https://github.com/dotnet/maui/blob/main/src/Core/src/Platform/Windows/Styles/Resources.xaml. The idea being that performance of the application would be better because the XAML would be set once and not for each and every label that sets some line height.

using System.Threading.Tasks;
using Microsoft.Maui.DeviceTests.Stubs;
using Microsoft.Maui.Storage;
using Microsoft.UI.Xaml;
Copy link
Contributor

Choose a reason for hiding this comment

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

It would be good to add a UI test containing

		<!-- Exercise line heights less than 1. --> 
		<Label BackgroundColor="Green" FontSize="20" Text="ÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑ"/>
		<Label BackgroundColor="Red" LineHeight="0.8" FontSize="20" Text="ÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑ"/>
		<Label BackgroundColor="Green" LineHeight="0.2" FontSize="20" Text="ÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑ"/>
		<Label BackgroundColor="Red" LineHeight="0.4" FontSize="20" Text="ÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑ"/>
		<Label BackgroundColor="Green" LineHeight="1.0" FontSize="20" Text="ÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑ"/>
		<Label BackgroundColor="Red" LineHeight="1.2" FontSize="20" Text="ÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑ"/>
		<Label BackgroundColor="Green" LineHeight="1.4" FontSize="20" Text="ÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑ"/>
		<Label BackgroundColor="Red" LineHeight="1.6" FontSize="20" Text="ÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑ"/>
		<Label BackgroundColor="Green" LineHeight="2.0" FontSize="20" Text="ÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑ"/>
		<Label BackgroundColor="Red" LineHeight="4.6" FontSize="20" Text="ÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑ"/>
	</VerticalStackLayout>

from the comment #24520 (comment)

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Windows] Change the LineStackingStrategy to BlockLineHeight for Labels on Windows

3 participants