How to remove additional padding from a WPF TextBlock? - c#

By default a WPF TextBlock seems to have additional top and bottom padding applied. I wish this wasn't so.
I've tried setting negative padding, but got an exception:
0,-10,0,0' is not a valid value for property 'Padding'.
I've tried setting the LineHeight property, to no apparent effect.
This is how the TextBlock looks in Blend. I've marked the problematic portion with maroon red.

Some research and H.B. guided me to the right answer, which is setting the following properties:
<TextBlock LineStackingStrategy="BlockLineHeight"
LineHeight="20"/> <!-- Or some other value you fancy. -->

This is probably part of the font which is Segoe UI by default, try Segoe instead for example. (You cannot assign negative padding but you could assign negative Margins, e.g.: Margin="0,-3,0,0")

This space is not padding, but part of the font, reserved for accents above and below characters.
The accepted answer makes the line height smaller than the font height.

Related

How ti disable fallback font in WPF?

How to disable fallback font in WPF?
I want to display text in a font that doesn't support all characters, but VS keeps replacing missing characters with fallback values.
Example, if I want to display "test" in a font that doesn't have code points for 'e' and 's',
I want to show only "tt" not "tESt" where 'E' and 'S' are in some other, fallback, font. Which is what is happening right now.
I've tried:
<TextBlock Text="{Binding SampleText}" FontFamily="{Binding FFamily, FallbackValue={x:Null}}"/>
but it doesn't work.
Any ideas?
Wpf has a default fallback font.
You may replace this with one of your own.
https://learn.microsoft.com/en-us/dotnet/api/system.windows.media.fontfamily?view=netcore-3.1
If you use a zero width ttf then i think that would give the result you're looking for.
In that each glyph is deliberately zero width.
I don't have a font missing glyphs to try this with, but it looks like it'd work.
https://github.com/patrickkunka/zero-width/blob/master/README.md

richtextbox is shrinking at run time however textbox is not

i'm at a loss as to why my parameter richtextbox is resizing its height at runtime. I'm assuming that it has something to do with my high def displays.
all other fields are simply textbox.
any suggestions as to fields I can check? Form.AutoScale is set to font.
http://imgur.com/a/S0dq0
It seems that this issue is directly related to the use of the font "Microsoft YaHei". Changing the font to San Serif immediately resolved the issue.

WPF character spacing irregular in RichTextBox

I have a problem with character spacing.
Basically I have something like this which comes from a txt file:
****************
*System Details*
****************
Looks nice and uniform, however, when I open have this go into a RichTextBox this happens:
Irregular character spacing example:
I've tried all different properties to try and stretch it, render it etc. but nothing works.
The data is coming in from code-behind OpenDialogBox that stores all the lines of the file in a string[]. A foreach loop then sends the lines into the RTB. (It needs to be a loop as each line gets checked)
Any help it greatly appreciated!
Many thanks
This is most likely a font choice problem. By default WPF uses Segoe UI on Windows 7 and above which is a non-monospaced font. This means that each character will not necessarily take up the same amount of space as each other character leading to issues if you are trying to align characters between lines. The easiest way to get alignment to work is by changing the font to a monospaced font by setting the FontFamily property on the RichTextBox.

Change font size to fit textblock

I'm having a little problem, i have Custom list box item, that is 165 px height. So, the text block wraps text and gets 50% height of the list box item, example:
How you noticed the text size is too big.
Is there a way to make fint size exatly big to fit in the textblock?
As far as my understanding it can be done with help of textBox.Text.Length property. Also It depends on the font you are using.
What happens when the Text is 10000 characters long? Rather than trying to show all of the text you should make the font smaller (no need for something that large) and use TextTrimming. TextTrimming will cut the text short and put an ellipse at the end.
<TextBlock Text="{Binding MyProperty}" TextTrimming="WordEllipsis" />

Limit text to width of TextBox

I'm using a TextBox to create an editable title on something that looks like a post-it note. I changed to a TextBox from a RichEditBox to see if that could solve my problem, which it didn't, so I'm willing to change back if that helps.
My problem is that I don't want the user to be able to enter more characters than fit in the set width of the TextBox, because I want the whole title to be visible. Setting a fixed limit to the amount of characters that can be entered doesn't really work since for example 10 large M's would fill the width of my TextBox, but other characters will only fill half of it. So I would like to compare the width of the text to the width of the TextBox, so I can restrict input beyond that point.
Edit: I'm using the Segoe UI font, and I don't really consider changing the font to one with characters of equal width as a solution.
Set the TextBox Font to a Courier Font, then all characters are equal width and calculating max becomes trivial.
I am suck in programming world. i m try help you..

Categories

Resources