Is there's a function that can draw text and fit it inside a rectangle (the function will make the size of the text smaller as appropriate to make it fit or something).
I checked the parameters for DrawText() but I don't think it supports such a feature.
Maybe you can use GetTextMetrics to check if the text fits inside the rectangle, and if it doesn't, reduce the current font size and repeat the measurement.
GetTextMetrics:
http://msdn.microsoft.com/en-us/library/windows/desktop/dd144941%28v=vs.85%29.aspx
There is no function doing it right away, but your can write your own using DrawText with DT_CALCRECT parameter. This parameter will only calculate size of your text and return it to you. It allows also multi line text and also checks provided maximal width you can allow text to be. So if the output rectangle - as calculated by DrawText is too large, then you must calculate it again but with smaller font, you can speed up calculations by using binary method to find the most appropriate font size.
Related
I need to measure the exact pixel size of a string in label control in Winforms.
I tried with TextRenderer.MeasureText(Text, Font) and graphics.MeasureString(Text, Font) but didn't get exact pixel width.
When checked the exact pixel width it is 90 but TextRenderer and Graphics are giving different values.
Can anyone help me on this.
Please note what is written in the TextRendered.MeasureText documentation (emphasis mine):
Return Value
Type: System.Drawing.Size
The Size, in pixels, of text drawn on a single line with the specified font. You can manipulate how the text is drawn by using one of the DrawText overloads that takes a TextFormatFlags parameter. For example, the default behavior of the TextRenderer is to add padding to the bounding rectangle of the drawn text to accommodate overhanging glyphs. If you need to draw a line of text without these extra spaces you should use the versions of DrawText and MeasureText that take a Size and TextFormatFlags parameter. For an example, see MeasureText(IDeviceContext, String, Font, Size, TextFormatFlags).
Also, please note that the Graphics.MeasureString documentation (again, emphasis mine):
Remarks
The MeasureString method is designed for use with individual strings and includes a small amount of extra space before and after the string to allow for overhanging glyphs.
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 have a string to draw in a custom dialog box. How can i get the required length of string in pixels using WPF?
If you want to show it afterwards within a TextBlock, create the TextBlock and call Measure and Arrange. Make sure that the TextBlock has set the right font size before calling Measure.
Another way is to go via FormattedText, if you want to do your calculations on a low level.
You might not need the (pixel) size.
It might be better to automatically size the dialog to its content.
This is the inverse of Determine Label Size based upon amount of text and font size in Winforms/C#.
Given a rectangle of fixed height but variable width, is there an efficient way to determine the largest size of a given font that will fit in the rectangle (height-wise) without risk of losing ascenders/descenders?
I'm currently considering iterative use of MeasureString() to find the best font size but wonder if there's a better way.
I've never found a better way to do it than using MeasureString iteratively.
You can optimise by jumping in increments.
WPF has some nicer text sizing options, though it feels like rubbing salt in the wound.
You don't really need iterations. Since the total width is also proportional to the font size, you only have to measure the string once with any reasonable sized font.
Afterwards, you can calculate your required font size:
fontSizeUsedToMeasure*(RectangleWidth/MeasuredWidth)(*0.8 for a nicer fit)
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.