-
Notifications
You must be signed in to change notification settings - Fork 1.9k
[Windows] Fix LineHeight values < 1 having no effect by setting LineStackingStrategy to BlockLineHeight #31219
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
Conversation
Co-authored-by: PureWeen <[email protected]>
Co-authored-by: PureWeen <[email protected]>
4212eba to
6045060
Compare
| if (label.LineHeight >= 0) | ||
| { | ||
| platformControl.LineHeight = label.LineHeight * platformControl.FontSize; | ||
| platformControl.LineStackingStrategy = LineStackingStrategy.BlockLineHeight; |
There was a problem hiding this comment.
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; |
There was a problem hiding this comment.
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)
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.