WPF TextBlock Cutoff - wpf

Hi Guyz I have a WPF TextBlock of fixed width say 100 , If the string doesnt fit in the width the last character is being cutoff always as all the characters are of not the same size. I dont want to cut the character instead I want to skip the text from there and just display the text with no character cutoff.

You have a couple of options to control wrapping and cutting of text:
TextWrapping can be used to make the text flow to the next line
TextTrimming can be used to decide how to cut text that doesn't fit
TextTrimming=None (the default) will mean that text which doesn't fit will be hidden, but it may cut down the middle of a character, which sounds like the problem you describe.
TextTrimming=WordEllipsis or TextTrimming=CharacterEllipsis will avoid showing half a character, but will append "..." to the end of the text. That will probably look better to users.
If you want to cut off the extra characters without adding the ellipsis, you'd have to use the technique Ed S. described

I suppose that I don't really understand your use case here. My first suggestion would be to simply dynamically size your TextBlock. If that's not possible then you wil have to get the width of the string and manipulate it yourself before you set it in the TextBlock (or use a fixed width font assuming that you can and you know the max length of the string).
If you need to measure the width of the string before it is displayed you can use the FormattedText class to do so.

Related

GDI+ Graphics.DrawString with TextTrimming set to EllipsisCharacter doesn't work if output rectangle contains one character

We use the classic GDI+ DrawString method to output text and set the Trimming property of its format parameter to EllipsisCharacter (see also the StringTrimming enumeration). If the output rectangle is enough small so it can display just one character, the ellipsis is not present on the screen - which misleads the user:
Is there a way to solve this problem?

How to make a gtktextview drop to the next row when getting to the border of the window?

I want to make a textview that will output strings that may be multiple rows long, and to divide them when they reach the border of the window. The window is set to non-resizable, so its size is fixed.
I tried to get the column and row numbers and work with these, but each character has a different size in pixels, so while a row full of "m" characters would reach the end of the row with the tenth character, a row of "0" would need about sixteen characters to get to the end of the line.
Any ideas?
It seems that you want to implement line wrapping in a GtkTextView. You don't need to do it yourself, the text view widget already supports line wrapping. To turn it on, call gtk_text_view_set_wrap_mode() with the text view and the appropriate wrap mode, such as GTK_WRAP_CHAR.

How to align the text in textbox, seems different character has different width

For multiline TextBox in wpf, i find the length of the chars of the text is not always equal.
For instance, if i put kkk in the first line, "f f" in the 2nd line, they wont align...since f and space take smaller size.
How to stop that and make every char has the same size??
So that my strings can be align?
Set the font to Courier New or another monospace font.

How to render whitespaces as dots in WPF RichTextBox?

I need the way to render regular space, nonbreaking space and some other formatting characters (like left-to-right mark) like MS Word renders them, when you choose to show non-printable characters.
I tried two approaches:
1) Replace characters with rarely used characters.
It works, but in this case we loose "nonbreaking" behavior of nonbreakable space (and LTR and RTL marks also stop working)
2) Use the custom font
It allows to preserve special behavior of nonbreaking space and LTR/RTL marks, but for some strange reason WPF renders nonbreaking space with usual space glyph.
WinForms RichTextBox renders text with the same font correctly.
This problem could be solved with applying different font with different space glyph for spaces and nonbreaking spaces, but LTR and RTL marks are not rendered at all even if I add glyph for them.
Have you any ideas how I could render that characters with visible glyph preserving their "LTR", "RTL", "nonbreaking" behavior?
I didn't try anything similar until now, but I can think of two options:
Warning -> I didn't try it out
The first method:
Create a subclass of UIElement
Get the Style with ControlTemplate for the Richtextbox and add it to App.xaml
Add an instance of your subclassed UIElement within the inner Panel of the Scrollviewer from the RichTextBox ControlTemplate
Make the RTBox available to a dependency property in your class via DataBinding in the ControlTemplate (if possible) or any other way that does the job
In your UIElement subclass, you iterate through characters of the document
Draw a symbol in your Adorner for each space and LineBreak you encounter
Get the Rect of a character at a specific position with the RichTextBox. Use this rect for placing the symbols.
The advantage of this Method is that you have a clean separation and don't need to subclass the RTFBox, but you won't be able manipulate the width of the spacing to make room for larger symbols. Also, other developers need to know that they need that Style in order to gain that functionality.
The second method:
Create a Custom Adorner
Decorate the RTBox with the custom Adorner
From the Adorner, you should be able to access the Child RTBox
In your UIElement subclass, you iterate through characters of the document
Draw a symbol in your UIElement for each space and LineBreak you encounter
I remember that there is a possibility to get the Rect of a character at a specific position with the RichTextBox. Use this rect for placing the symbols.
It's also without subclassing the RTBox. You also can't adjust the spacing. In contrast to method 1, other developers will immediatly recognize that this functionality has been added. The one disadvantage is that you will have to handle scrolling too.
You can try insert near LTR/RTL visible glyph instead of replace that.
Store all of the values as their special characters. It sounds like your printing functions need to handle a) what kind of output the user wants, b) interpret your data array/massive string of characters and spit out the values with regard to what the user wants to see. You don't give too many details on how your things are stored but this would be a very reasonable way to go about things.

Resizing Labels

I have a chart in WPF with a lot of labels. The text on these labels is dynamically loaded and subject to change. If I set the width just to auto, then these labels may overlap, which makes the text unreadable.
The chart support multiple sizes, so if it gets larger, then the bars are re sized and there is more space for text. Now I want to adjust the text to the space which is available. If it gets too small, I don't want to display the label anymore (a tooltip is available, so the user still gets the required information). Consider the string "Case 1, blah blah", there is probably not enough space to display the whole string, but just the first word. In this case I want the string to be "Case 1..", with .. indicating that there is some more information in the tooltip.
I can determine the length available for the string. But how can I determine the space a single letter will take? Of course I could also just re size the label, but then it would just cut off the string anywhere which is probably not helpful for the user (and looks ugly).
Any ideas?
If you can use TextBlocks instead of labels then they have a TextTrimming property which will do this for you to either the nearest character or the nearest word.
While you seem happy with the TextTrimming property, I'll edit this to add that the TextBox control has a GetRectFromCharacterIndex method that would allow you to find out the size on screen of one or more characters as long as the font settings matched your label. This might be useful if you wanted to trim at specific places in the label rather than the nearest character / word.
Not an expert in WPF, but I would think that you'll need to do this in code rather than XAML.
Start by obtaining the actual pixel width of the space available for the text.
Then look at the character set, dot pitch etc. utilised on the XAML front end and from there calculate the pixel width required per character.
You could also look at changing the character sizes as well as reducing the label length.

Resources