Reputation: 508
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:
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.
Upvotes: 4
Views: 2344
Reputation: 9309
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
Upvotes: 4
Reputation: 1001
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.
Upvotes: 0