Answered by:
How to measure the size of a string

Question
-
windows app store how to measure the size of a string?
string str = "Hello word windows app store";
- Edited by oneonce Friday, March 6, 2015 2:41 PM
Friday, March 6, 2015 12:44 PM
Answers
-
Size MeasureString(string content, Size availableSize, double fontSize, string fontFamily="Segoe UI") { TextBlock tb = new TextBlock(); tb.TextWrapping = TextWrapping.Wrap; tb.Text = content; tb.FontFamily = new Windows.UI.Xaml.Media.FontFamily(fontFamily); tb.FontSize = fontSize; tb.Measure(availableSize); Size actualSize = new Size(); actualSize.Width = tb.ActualWidth; actualSize.Height = tb.ActualHeight; //return tb.DesiredSize; return actualSize; }
actual size
- Marked as answer by oneonce Friday, March 6, 2015 2:12 PM
Friday, March 6, 2015 2:11 PM
All replies
-
if you need the actual width the string occupies in pixels than use (see this MSDN link for more details):
Graphics.MeasureString(String, Font)
if you only need the "length" of the string i.e. no. of characters than use (see this MSDN link for more details):
str.Length
dont forget to bing / google :)
hope this helps!
Friday, March 6, 2015 12:53 PM -
There is no such thing as the "width" of a string variable or string value. You can get the length, i.e. the number of characters in the string, using the Length property:
string str = "Hello word windows app store"; int length = str.Length;
If you want to get the width of a TextBlock element that displays the string on the screen in your application you could use the ActualWidth property of the TextBlock element:
<TextBlock x:Name="byTBlock" Text="Hello word windows app store"/>
double width = byTBlock.AcualWidth;
Hope that helps.
Please remember to markclose your threads by marking helpful posts as answer and please start a new thread if you have a new question.
Friday, March 6, 2015 1:34 PM -
if you need the actual width the string occupies in pixels than use (see this MSDN link for more details):
Graphics.MeasureString(String, Font)
if you only need the "length" of the string i.e. no. of characters than use (see this MSDN link for more details):
str.Length
dont forget to bing / google :)
hope this helps!
Graphics.MeasureString(String, Font) can not use in windows store app.
TextBlock has Measure() method, as following:
Size MeasureString(string content, Size availableSize, double fontSize, string fontFamily="Segoe UI")
{
TextBlock tb = new TextBlock();
tb.TextWrapping = TextWrapping.Wrap;
tb.Text = content;
tb.FontFamily = new Windows.UI.Xaml.Media.FontFamily(fontFamily);
tb.FontSize = fontSize;
tb.Measure(availableSize);
return tb.DesiredSize;
}Test:
string str = "Hello word windows app store";
Size size = MeasureString(str, new Size(800, 600), 40);
Friday, March 6, 2015 2:07 PM -
Size MeasureString(string content, Size availableSize, double fontSize, string fontFamily="Segoe UI") { TextBlock tb = new TextBlock(); tb.TextWrapping = TextWrapping.Wrap; tb.Text = content; tb.FontFamily = new Windows.UI.Xaml.Media.FontFamily(fontFamily); tb.FontSize = fontSize; tb.Measure(availableSize); Size actualSize = new Size(); actualSize.Width = tb.ActualWidth; actualSize.Height = tb.ActualHeight; //return tb.DesiredSize; return actualSize; }
actual size
- Marked as answer by oneonce Friday, March 6, 2015 2:12 PM
Friday, March 6, 2015 2:11 PM