View previous topic :: View next topic |
Author |
Message |
cmosses
Joined: 05 Oct 2007 Posts: 6
|
Posted: Fri Oct 05, 2007 9:19 am Post subject: Measuring multiline strings |
|
|
Hello, great component, but I have this problem:
I need to draw a number of items in a list, and the list is quite narrow, so the items typically don't fit on a single line. I would like to measure how many lines a text fills when wrapped, but XGraphics.MeasureString does not have an overload that lets me specify maximum line width. It can only tell me the width of the text on a single line, not when broken into multiple lines.
I can do an XGraphics.DrawString and specify a layout rectangle, and that works fine, wrapping the lines perfectly, but afterwards I have no idea how many lines were filled. Thus, I don't know where to start the next list item on the page.
Is there any other way I could do this? Any ideas?
Can anyone please help? Thanks a lot in advance. |
|
Back to top |
|
|
mohali
Joined: 23 Oct 2007 Posts: 2
|
Posted: Tue Oct 23, 2007 7:24 pm Post subject: |
|
|
I've a same problem. Did you measure no of lines. I need to know when the what is next line. Also when string is long.
Thanks,
-mak |
|
Back to top |
|
|
cmosses
Joined: 05 Oct 2007 Posts: 6
|
Posted: Wed Oct 24, 2007 1:50 am Post subject: |
|
|
mohali wrote: | I've a same problem. Did you measure no of lines. I need to know when the what is next line. Also when string is long.
Thanks,
-mak |
I wrote a method as a workaround, which reliably measures multiline strings within a maximum layout rectangle. I use the primitive MeasureString that we are given, measuring a single line as I gradually add words to the end of it. When it is full within the max line width, I start a new line and start measuring again, and so forth. The height is the number of lines times the font height. Hope this helps. |
|
Back to top |
|
|
mohali
Joined: 23 Oct 2007 Posts: 2
|
Posted: Wed Oct 24, 2007 1:05 pm Post subject: |
|
|
can you give me sample code?
thanks,
rgds,
-mak |
|
Back to top |
|
|
cmosses
Joined: 05 Oct 2007 Posts: 6
|
|
Back to top |
|
|
ACS
Joined: 01 Dec 2007 Posts: 15
|
Posted: Sat Dec 08, 2007 2:43 am Post subject: |
|
|
Thanks for the code, cmosses.
However there is a problem with that code that causes the method to return the wrong height (it will be off by 1 lineCount.)
The bug is not with the code itself, but rather with the .NET Framework's MeasureString method, which is used by PDFSharp's MeasureString method. The problem is that MeasureString will automatically trim the string being measured, eliminating any spaces. This means that " Hello, world! " will be measured as "Hello, world!". This is not a problem for single-line strings, but for multiple-line strings it may cause incorrect results.
I discovered this recently during a project and I found a cheapo way to resolve it, and that is by using a period character instead of a space, which is approximately the same width (at least with the few fonts that I've tested.) This will result in perfect MeasureString results each time.
If anyone's interested in this code, let me know and I'll post it. |
|
Back to top |
|
|
cmosses
Joined: 05 Oct 2007 Posts: 6
|
Posted: Thu Dec 13, 2007 8:43 am Post subject: |
|
|
ACS wrote: | Thanks for the code, cmosses.
However there is a problem with that code that causes the method to return the wrong height (it will be off by 1 lineCount.)
The bug is not with the code itself, but rather with the .NET Framework's MeasureString method, which is used by PDFSharp's MeasureString method. The problem is that MeasureString will automatically trim the string being measured, eliminating any spaces. This means that " Hello, world! " will be measured as "Hello, world!". This is not a problem for single-line strings, but for multiple-line strings it may cause incorrect results.
I discovered this recently during a project and I found a cheapo way to resolve it, and that is by using a period character instead of a space, which is approximately the same width (at least with the few fonts that I've tested.) This will result in perfect MeasureString results each time.
If anyone's interested in this code, let me know and I'll post it. |
Pass an XStringFormat with the flag XStringFormatFlags.MeasureTrailingSpaces to solve this (I think). |
|
Back to top |
|
|
ilivewithian
Joined: 07 May 2008 Posts: 4
|
Posted: Wed May 07, 2008 1:55 pm Post subject: |
|
|
I've noticed a small bug with the multiline MeasureString, it doesn't take into account \n in strings, so the number of lines will be wrong if a new line is in the code. _________________ My wife the classical singer |
|
Back to top |
|
|
|