Skip navigation.
 
mlRe: horizontal sizeToFit of NSTextView or NSTextField
FROM : Manfred Schwind
DATE : Mon Apr 21 20:03:58 2008

> Either I'm misunderstanding your question, or you're 
> misunderstanding the Geometries code.  I think you want -
> widthForHeight:attributes:, with the height specified for three 
> lines (could just take it from what IB sets for you, or first get a 
> heightForWidth:reallyBig, then triple that).  The demo program has a 
> -heightForWidth:attributes: case, just flip it around.  This code 
> _does_ work for a multi-line height, and does allow for correct 
> wrapping, font changes in mid fly, multibyte characters, and all the 
> rest.


No, the code does not work for multi-line height. If the text that has 
to be layouted does _not_ have newlines (so if I have one long line of 
text), then widthForHeight just returns the width of the text as if it 
is layouted in one long line. But I want the minimal width that is 
necessary so that the text is wrapped around into three lines.

I think this is the missing point: my text has no line breaks. It is 
one long line. But I am searching for a rectangle where the text is 
wrapped around into 3 lines and I am searching the optimal width for 
that.

OK. So I took NS(Attributed)String+Geometics and put the following 
test code in:

{
NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys:
                                           [NSFont systemFontOfSize:12.0], NSFontAttributeName,
                                           nil];
NSString *test = @"This is a very long line. It should be wrapped into 
three lines. And I want to get the optimal (minimal) width for that.";
float width = [test widthForHeight:45.0 attributes:attributes];    // a 
height of 45.0 is capable of holding 3 lines of 12pt system font
NSLog(@"width = %f", width);
// this is what widthForHeight internally does:
NSSize size = [test sizeForWidth:FLT_MAX height:45.0 
attributes:attributes];
NSLog(@"size = %@", NSStringFromSize(size));
}

Output is:

2008-04-21 19:40:20.317 StringGeometricsDemo[17913:10b] width = 
686.863281
2008-04-21 19:40:20.319 StringGeometricsDemo[17913:10b] size = 
{686.863, 15}

So, this is really NOT what I want. This is the width for the text if 
it is layouted in one line.

In the meantime I have a solution that searches for the optimal width 
with a binary search algorithm (just takes a few layouts to get a good 
result).
The result that I get for the text above is {246, 45}, and that's 
exactly the result that I wanted.
246 is the smallest width so that the text fits completely into the 
rectangle and is wrapped into 3 lines.
If I choose a smaller width, the text will be clipped (it will not fit 
completely in the rectangle).
If I choose a wider width, the text will be wrapped in less than 3 
lines at some point.

Regards,
Mani
--
http://www.mani.de
iVolume - Loudness adjustment for iTunes.
LittleSecrets - The encrypted notepad.

Related mailsAuthorDate
mlhorizontal sizeToFit of NSTextView or NSTextField Manfred Schwind Apr 19, 17:31
mlRe: horizontal sizeToFit of NSTextView or NSTextField Jack Repenning Apr 20, 06:26
mlRe: horizontal sizeToFit of NSTextView or NSTextField Manfred Schwind Apr 20, 20:37
mlRe: horizontal sizeToFit of NSTextView or NSTextField Gary L. Wade Apr 21, 02:08
mlRe: horizontal sizeToFit of NSTextView or NSTextField Jack Repenning Apr 21, 17:41
mlRe: horizontal sizeToFit of NSTextView or NSTextField Manfred Schwind Apr 21, 20:03