[Windows] Fix LineHeight values < 1 having no effect by setting LineStackingStrategy to BlockLineHeight #31219
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.