I'm sure I'm missing something obvious, I have an area in which I intend to draw text. I know its (the area) height and width. I wish to know how many characters/Words will fit in the width, characters preferably. Second question, If the line is too long I'll want to draw a second line, so I guess I need to get the height of the text as well, including what ever it considers the right vertical padding?
I'd also rather like to know the inverse, i.e. how many characters I can fit in a specific width.
I assume the fact that WPF isn't constrained to pixels will have some bearing on the answer?
Ultimately I'm planning on wrapping text around irregular shaped images embedded in the text.
Any pointers in the right direction would be great.
Thanks
@BrokenGlass's answer is great, but depending on the characteristics of your application, you may find you get better performance with a binary search. If the majority of your strings fit in the available width, or generally only need to be trimmed by a character or two, then a linear search is best. However, if you have a lot of long strings that will be severely truncated, the following binary search will work well.
Note that both availableWidth and fontSize are specified in device-independent units (1/96ths of an inch). Also, use the TextFormattingMode that matches the way you draw your text.
This is impossible because characters have different length, for example W is much wider than i (unless you're using a font like Courier New).
For WPF you can use the FormattedText class to calculate how much width a given text string will use - it will depend on the actual text.
Example:
Getting a sub string for a given width (simplified):