On 04.09.2008, at 03:57, an0 wrote:
IMO, the size of a string with font attribute is an intrinsic property
determined only by the string and the font(and the layout of the
string on the view, but here let's fix the layout to one single line);
and the view used to render the attributed string should have a
reasonalby bigger size to hold the whole string. In other words, one
could have a good guess of the view size from the attributed string
size.


Well, this is programming, we try not to guess too much. It's much better design to ask the object that actually does all the drawing to tell you how much room it will take. Consider an NSTextFieldCell: It can have no borders, rounded ends (in which case the distance from the left and right edges to the actual text will be larger than that to the top and bottom, to accomodate the half-circles), rectangular border... If this measuring was done by string dimensions and a reasonable guess, it'd mean if you realize field X should really have rounded ends instead of straight ones, you'd have to change your measuring code everywhere you do your reasonable guess.

On the other hand, the NSTextFieldCell knows how it has to be configured, and it knows what algorithm to use for its current appearance and content. It has to, or it wouldn't be able to draw itself. Why duplicate this logic elsewhere? A cell is a lightweight object intended to encapsulate text drawing, selection and editing, and creating one and keeping it around means your drawings will be much faster, because state and previous results of measurements can be cached in the NSTextFieldCell for faster drawing.

That said, while Cocoa has a convenience method for drawing a string in a rect in NSStringDrawing.h, there's none for measuring it. You could roll your own according to this article:

        http://www.zathras.de/blog-cocoa-text-system-everywhere.htm

but for best performance you'd want to cache the three text system objects you created for this (the NSStringDrawing methods can't really do much cacheing, though I've heard they try their best given their lack of information), and that's what an NSTextFieldCell probably does anyway, so why bother? Just use the cell class for your string measuring and drawing.

Cheers,
-- Uli Kusterer
"The Witnesses of TeachText are everywhere..."
http://www.zathras.de





_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to [EMAIL PROTECTED]

Reply via email to