Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to alter the height of a textblock

Tags:

wpf

xaml

The width of the background of a textblock that surrounds the text is very narrow. However, the top and bottom is very large. How do I shrink this space down? I can't do it by setting the height, and I can't attach a negative padding to it.

 <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"></RowDefinition>
        </Grid.RowDefinitions>


        <TextBlock  FontSize="200"                                 
                    FontWeight="Bold"
                    Background="Black"
                    Foreground="White"  
                    Text="bla bla"
                    HorizontalAlignment="Center">
        </TextBlock>
    </Grid>

enter image description here

like image 996
nikotromus Avatar asked Oct 15 '25 04:10

nikotromus


2 Answers

You can use the Height of the element to reduce the space at the bottom, you can combine TextWrapping, LineHeight and LineStackingStrategy to adjust the space at the top of the element:

<Grid>    
    <TextBlock  FontSize="200"                                 
            FontWeight="Bold"
            Background="Black"
            Foreground="White"  
            Text="bla bla"
            Padding="0"
            Margin="0"
            HorizontalAlignment="Center"
            TextWrapping="Wrap" 
            LineStackingStrategy="BlockLineHeight"
            LineHeight="200" 
            Height="180">
    </TextBlock>
</Grid>

Result:

Result

like image 104
Isma Avatar answered Oct 17 '25 18:10

Isma


The property you need to set is LineHeight, combined with LineStackingStrategy = "BlockLineHeight"

<TextBlock FontSize="72" 
    Background="Black" 
    Foreground="White" 
    FontWeight="Bold" 
    Text="Hello World" 
    LineHeight="72" 
    LineStackingStrategy="BlockLineHeight" /> 
like image 27
Peregrine Avatar answered Oct 17 '25 18:10

Peregrine



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!