[Windows] Fix LineHeight values < 1 having no effect by setting LineStackingStrategy to BlockLineHeight#31219
Closed
[Windows] Fix LineHeight values < 1 having no effect by setting LineStackingStrategy to BlockLineHeight#31219
Conversation
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
Co-authored-by: PureWeen <5375137+PureWeen@users.noreply.github.com>
Co-authored-by: PureWeen <5375137+PureWeen@users.noreply.github.com>
4212eba to
6045060
Compare
MartyIX
reviewed
Aug 18, 2025
| if (label.LineHeight >= 0) | ||
| { | ||
| platformControl.LineHeight = label.LineHeight * platformControl.FontSize; | ||
| platformControl.LineStackingStrategy = LineStackingStrategy.BlockLineHeight; |
Contributor
There was a problem hiding this comment.
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.
MartyIX
reviewed
Aug 19, 2025
| using System.Threading.Tasks; | ||
| using Microsoft.Maui.DeviceTests.Stubs; | ||
| using Microsoft.Maui.Storage; | ||
| using Microsoft.UI.Xaml; |
Contributor
There was a problem hiding this comment.
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)
This was referenced Aug 19, 2025
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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
LineHeightto values less than 1 had no visual effect on Labels. The problem was that WinUI TextBlock's defaultLineStackingStrategyisMaxHeight, 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:
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 heightLineStackingStrategy: Determines how the LineHeight value is appliedThe default
LineStackingStrategy.MaxHeightuses the larger of:This means LineHeight values < 1 are effectively ignored since the natural height is typically larger.
Solution
Modified the
UpdateLineHeightmethod inTextBlockExtensions.csto setLineStackingStrategytoBlockLineHeightwhen a LineHeight is specified.BlockLineHeightalways uses the exact LineHeight value, allowing values < 1 to take effect.Additional Changes
#if !WINDOWSconditionals)GetNativeLineHeight()andGetNativeCharacterSpacing()methods for Windows test infrastructureTesting
The fix ensures consistent LineHeight behavior across all platforms:
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.