How do you calculate pixel size of string? - c#

I have a string. I know the font family and the font size it is going to be displayed in. I need to know how many pixels the text will take up in the ui. So that I can determine whether to show another element. How would I do that?
I found a couple of things, but none of them were available in my Windows universal project. For example:
Graphics.MeasureString
TextRenderer.MeasureText
Edit:
This is not a web project.
I want to calculate the size it will take in the ui before it is in the ui.

I think you need to create a textblock in code and assign the desired text to it. Then you can get the actual height and width from it. See the below code
TextBlock txt=new TextBlock();
//set additional properties of textblock here . Such as font size,font family, width etc.
txt.Text = "your text here";
var height = txt.ActualHeight;
var width = txt.ActualWidth;
You can do further operations based on this height and width
I am not saying this is the optimized solution .But this will work for you

Try checking the values of the Width and Height properties of the control you use to display your text (eg. your TextBox), after setting your string as text/content, to decide whether to show another element.

Related

How to fix TextBox height in C#

Hi, guys..
I'm working on a small project with C# language in Visual Studio 2010 for my own business (Not using WPF). So, In the designer, I normally added a TextBox from ToolBox to the Form and, then normally changed the name of the TextBox from Properties Box. After that, I wanted to change Font Name & Size (The default values for Font name and size are "Tahoma" & "8.75" respectively). So, when I changed the Font name to " Times New Roman", the size of the Font was small, So I decided to change it From the default value to "11.5". So, when I changed the Font size to required value, it got changed, but the TextBox height increased. So, How can I change the Font size for the TextBox to get bigger without any effect on the height property?, In other words, how to fix the TextBox height, when I changed the Font size to get bigger?..
Thank you so much for your interest, I hope I got the problem clearer..
(Note: I haven't used a code yet. Just from the Designer)
The TextBoxBase.AutoSize property determines whether the height of the TextBox will change to fit the the size of the font. If you set it to false, you can change the height to whatever you want regardless of the font size.
This property is not visible in the Properties window, so you will have to set it in code. If the name of your TextBox is textBox, then:
textBox.AutoSize = false;

Dynamic sized multi line TextBox field in a PDF

I have created a document in an open office with a multi-line form field:
The issue I am having is when the dynamic content exceeds the initial size of the multi-line text box:
Sure I can re-size the Text Box in the original template but the dynamic content may be from 1 to 50 lines and I want the text after the Text Box to be close to the last line of dynamic content.
Can someone suggest a way to solve this?
I have once coded a solution to set the width and height of a textbox programmatically based on the characters supplied. I think this was a school assignment a long time ago.
This can be done with both VBA in a code behind or probably with a macro even. Or with VB.net.
I don't have the code I used way back when, but basically determine the maximum width you are able to provide in character width, the preferred width. Determine the pixel requirement per character for that width. This becomes the textbox width.
Divide your total string character count by your preferred width character count. Round up, calculate the pixel height per character. And use this value times the rounded result for the textbox height. Dirty but it should work.
Any chance you can change to a label and set AutoSize to True? You can fix the width and let the height auto adjust. This should be done before converting to a pdf. In fact all of the sizing should be resovled before pdf conversion.
Another Down voter without a comment, should not be allowed.

Determine Control's 'AutoSize'-size at runtime

I want to layout controls during runtime (dynamically created). For the purpose of this question, let's restrict to a Button control. I want to set the control's properties (such as Text) and then determine the minimum size for the control for it to display properly; the size that setting AutoSize = true would give. In C# example code, with GetAutoSizeSize being this minimum size:
Button button = new Button();
this.Controls.Add(button);
button.Text = "Example";
button.Size = GetAutoSizeSize(button);
button.Location = /* Some calculation based on button.Size */
Possible solution: AutoSize
One can set button.AutoSize = true and button.AutoSizeMode = AutoSizeNode.GrowAndShrink. After that, the button.Size can be fetched, after which AutoSize can be turned off and the control size can be changed.
Potential issues:
It looks odd and I can't help but feel that this could easily break, but maybe I am wrong?
Possible solution: GetPreferredSize
button.GetPreferredSize can be used to get a size that the control wants to be.
Problems with this:
Its usage is internal and/or meant for flow layout.
GetPreferredSize takes a suggested size as a parameter, so one needs to guess at what would be appropriate.
The size returned is wrong, in that it returns the 'comfy' size of a control, which can be much larger than the minimum size that AutoSize gives.
EDIT: From the comments and some trial-and-error, I was able to conclude that the problems I originally listed with the AutoSize-method were due to needing both the control to be added to the control collection first and AutoSizeMode set to GrowAndShrink.
I would like to know if there is a function (and/or more 'robust' way) of determining the AutoSize-size: a function like GetPreferredSize that returns the size without actually having to toggle AutoSize.
This works when you are drawing on a control.
String sMyString = "this is my string";
Font fntFont = new Font("Arial", 8);
SizeF sfMySize = new SizeF(5,5);
sfMySize = System.Graphics.MeasureString(sMyString, fntFont, sfMySize);
This will give you the dimensions of the bounding box around the control text. You would have to work out the appropriate buffer around the text to set the button size.

Get rid of ugly CustomEditor spacing

I've been making my own event management system because I don't want to pay money for Playmaker:
I'm getting this really terrible spacing between labels and their components (for example, in the picture above, between Nickname and it's text field)
The script can be found here (keep in mind, its a work in progress. I haven't had time to clean it all up):
http://pastebin.com/w2cLWBvh
It looks like that layout system you've been using aims to give both the nickname label and text input field nearly the same amount of space within their combined area.
Without knowing an awful lot about what layout options Unity's GUI system has, you can probably do with setting the length of the label and textbox arbitrarily.
Here's how you can get the size of a label given a specific string:
GUI.skin.GetStyle("Label").CalcSize(new GUIContent(widestIdString));
So say you have a new BeginArea containing your label and textbox combo. You'd set the label length to this newly calculated width, and the textbox width to the difference of the Area's width and the label's width, plus an arbitrary distance to seperate them, such as 23f;

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