I can set fixed height in pixels, but i'd like to set it in lines. Sort of like in html you can set height of an textarea to number of rows/lines.
A:
Try 3em
1em is equal to the current font size. 2em means 2 times the size of the current font. E.g., if an element is displayed with a font of 12 pt, then '2em' is 24 pt.
Catch22
2010-09-23 10:10:12
Does that take leading (WPF calls it LineHeight, iirc) into account? If not, then you maybe have less than three lines.
Joey
2010-09-23 10:17:00
according to MSDN only px, in, cm, pt is allowed, no em
Kugel
2010-09-23 11:03:19
perhaps you can convert it somehow
hkon
2010-09-23 12:40:51
accepted, my font is 12pt, setting height to 36pt worked
Kugel
2010-09-29 16:11:35
A:
Solution 1
You could FormattedText to measure the size of text, here is an example:
String text = "Here is my text";
Typeface myTypeface = new Typeface("Helvetica");
FormattedText ft = new FormattedText(text, CultureInfo.CurrentCulture,
FlowDirection.LeftToRight, myTypeface, 16, Brushes.Red);
Size textSize = new Size(ft.Width, ft.Height);
Solution 2
Use the Graphics class (found here ):
System.Drawing.Font font = new System.Drawing.Font("Calibri", 12, FontStyle.Bold);
Bitmap bitmap = new Bitmap(1, 1);
Graphics g = Graphics.FromImage(bitmap);
SizeF measureString = g.MeasureString(text, font);
Here you are !
Jmix90
2010-09-23 21:35:09