• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: horizontal sizeToFit of NSTextView or NSTextField
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: horizontal sizeToFit of NSTextView or NSTextField


  • Subject: Re: horizontal sizeToFit of NSTextView or NSTextField
  • From: Manfred Schwind <email@hidden>
  • Date: Mon, 21 Apr 2008 20:03:58 +0200

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.

_______________________________________________

Cocoa-dev mailing list (email@hidden)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden


References: 
 >horizontal sizeToFit of NSTextView or NSTextField (From: Manfred Schwind <email@hidden>)
 >Re: horizontal sizeToFit of NSTextView or NSTextField (From: Jack Repenning <email@hidden>)
 >Re: horizontal sizeToFit of NSTextView or NSTextField (From: Manfred Schwind <email@hidden>)
 >Re: horizontal sizeToFit of NSTextView or NSTextField (From: Jack Repenning <email@hidden>)

  • Prev by Date: Re: NSTableView -editColumn:row:withEvent:select: question
  • Next by Date: Re: Unregistering KVO observers-solved
  • Previous by thread: Re: horizontal sizeToFit of NSTextView or NSTextField
  • Next by thread: Re: horizontal sizeToFit of NSTextView or NSTextField
  • Index(es):
    • Date
    • Thread