Skip navigation.
 
mlRe: horizontal sizeToFit of NSTextView or NSTextField
FROM : Manfred Schwind
DATE : Sun Apr 20 20:37:15 2008

>> Now I want to be able to calculate the optimal width of the view so 
>> that the entire text is visible.

>
> This worked wonders for me:
>
> http://www.cocoabuilder.com/archive/message/cocoa/2008/3/31/202752


This also does not solve my problem.
Getting the height of a text for a given width is easy and I've done 
that countless times.
Also getting the width of a string (layed out in one line if it has no 
newlines) is no problem.

But I have a different problem. I try to give an easier explanation of 
my problem:

I have a text view that has a height that can hold three lines of 
text. Now I have one long string (without newlines). When putting this 
string into the text view, the text is automatically wrapped around. 
But maybe - for the initial width of the view - it will need four (or 
more) lines instead of just three. Now I want to calculate the optimal 
(minimum) width of the text view, to that the string is wrapped around 
in just three (or less) lines.

In the meantime I guess I have the solution. I don't think that there 
is really a way in Cocoa or Quartz that solves this problem. So I 
really have to "try" different widths, let the layout manager layout 
the text and see how much lines it needs. So I can do a "binary 
search" to get an ideal width.
Even if Cocoa or Quartz would have a method for calculating the width, 
it would internally also have to do such a binary search. I don't 
think that it's possible to calculate the perfect width in one step.
So I just do it myself and it works quite well.

Thanks for your attention,
Mani

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