Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Text stretch in WPF TextBlock

I want to stretch the text in WPF Textblock with out changing the font size of the textblock?

like image 976
user679530 Avatar asked Jun 08 '11 16:06

user679530


2 Answers

use a layout or render transform to scale your text in the X or Y direction depending on what you want

LayoutTransform causes the scale to be applied prior to the layout pass which means the element is rendered with the scaled size taken in to account. Whereas the RenderTransform applies the scaling after the layout pass so the element is spaced at normal size then the scale is applied.

Something like

<TextBlock Text="Foo">
  <TextBlock.RenderTransform>
    <ScaleTransform ScaleX="2" ScaleY="2" />
  </TextBlock.RenderTransform>
</TextBlock>
like image 198
Brad Cunningham Avatar answered Nov 15 '22 09:11

Brad Cunningham


To stretch text over the entire control and make it narrower, I use ViewBox and Layout Transform:

<DockPanel>
  <Viewbox>
    <Viewbox.LayoutTransform>
      <ScaleTransform CenterX="50" ScaleX="0.5" />
    </Viewbox.LayoutTransform>
    <TextBlock Text="Some random text."  HorizontalAlignment="Stretch" VerticalAlignment="Stretch" />
  </Viewbox>
</DockPanel>
like image 20
Jiri Stybnar Avatar answered Nov 15 '22 10:11

Jiri Stybnar