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.
Related
I have a XAML form with a TextBlock object that is set to a width of 500 and a height of 150 (for example).
I'd like to figure out a way to have the font size change automatcally depending on the text assigned to the object, such that it's as big as possible without overflowing the assigned bounds. Including word wrap as necessary and possible so that the text fills the available space both horizontally and vertically.
In other words, if the text is "Star" it would use a font size of 40 but for "superstar" it might get set to 18.45, using smaller text so the entire width of the object gets used. (The font's normal aspect ratio must be maintained.)
My experimenting with using a ViewBox hasn't provided the desired results as yet.
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.
In our Silverlight page, I got comboboxes with different values. When selected, some values are too long to fit in the collapsed combobox.
I would like it to ellipsise ("System Use..." with three dots in the end).
In the dropdown the full name of the value can be seen, but when collapsed I would like the selected value to ellipsise. It feels like an obvious thing, as a drop down generally affords more space than the collapsed control, but still I cannot find an obvious way to do this.
You might want to take a look at http://www.codeproject.com/Articles/37503/Auto-Ellipsis.
It's similar to Zenexar's suggestion, uses a more efficient reduction algorithm in the general case. But it also writes about other types of 'ellipsing', which might also suit your needs perhaps.
Take the selected UIElement. Compare the RenderedWidth and RenderedHeight to the DesiredSize, the latter of which will be adjusted based on how the element is cut off. Repeatedly remove characters, append ellipsis, and invalidate the layout until the DesiredSize is smaller than or equal to the RenderedWidth/RenderedHeight
I think the best way would be to check the length of the selected item. If the length is more than a predetermined character length, cut off the rest and append three periods.
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.
I have textBlock defined such that it fills the entire screen of the phone.
The textBlock is initialized with some data which cannot be displayed in the boundary and hence gets clipped.
I want to read the data which actually got rendered on the screen (i.e. whole data - clipped data).
Putting a breakpoint shows me that myNewTextBlock.Text contains the entire data that it was initialized with.
Thanks
You could look at using Measure and MeasureOverride to determine how much of the Text would fit in the available space.
You'll likely need to test various trimmed versions of the Text but it shouldn't be too tricky.