Re: horizontal sizeToFit of NSTextView or NSTextField
Re: horizontal sizeToFit of NSTextView or NSTextField
- Subject: Re: horizontal sizeToFit of NSTextView or NSTextField
- From: "Gary L. Wade" <email@hidden>
- Date: Sun, 20 Apr 2008 20:08:31 -0400
If you wish to vary both the width and height to be a more pleasing set of dimensions, similar to what an alert does, try word wrapping your text into a golden ratio rectangle using the area measurement generated by your single-line text measurement. Of course, based on a strict interpretation of what I'm reading from you, with the height bounded, keep in mind that a square that is one foot high and 18 feet wide has the same area as a square that is three feet high and six feet wide. Of course, since we're talking about text that isn't cleanly broken at character positions, you'll probably need some ceiling breaks on the width area recalculation.
>>> 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
>
_______________________________________________
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