Using a temporary Text container/storage/layout seems to fix the problem. See: https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/TextLayout/Tasks/StringHeight.html
The NS(Attributed)String+Geometrics category by Jerry Krinock is also handy: https://github.com/jerrykrinock/CategoriesObjC.git Cheers! Tony Pollard On 27 Jan 2012, at 11:26 am, Tony Pollard wrote: > Good thought Tim, but for some reason it doesn't work in this case (both on > 10.6 & 10.7). > > As you would expect, it is size/font sensitive so I can get the same failure > result (for the sample text given) with Times-Roman 11 point. Of the various > text samples, it's only 1 in 10,000 or so that don't get their height > calculated correctly. > > Tony > > > On 26 Jan 2012, at 6:59 pm, Tim Schröder wrote: > >> I had exactly the same problem and solved it by using the >> NSStringDrawingUsesDeviceMetrics option instead of >> NSStringDrawingDisableScreenFontSubstitution. >> >> Tim >> >> Am 25.01.2012 um 16:26 schrieb Tony Pollard: >> >>> Hi folks, >>> >>> I'm having a strange problem with NSAttributedString boundingRectWithSize >>> in getting the needed height for a fixed width. It works 99% of the time, >>> but it consistently underestimates the height for certain text (by >>> approximately one line). There doesn't appear to be a pattern for the text >>> that works and the text that fails (tested on 10.6 and 10.7). >>> >>> The code is: >>> >>> NSRect textRect = [attrString boundingRectWithSize:NSMakeSize(300.0, 0.0) >>> // Width 300, any height (max height also >>> fails) >>> >>> options: NSStringDrawingUsesLineFragmentOrigin | // Needed >>> for multi-line >>> >>> NSStringDrawingDisableScreenFontSubstitution]; >>> >>> The attributed strings are built from data sources. Here's one that fails: >>> >>> Printing description of attrString: >>> n. { >>> CdctPhraseType = Grammer; >>> NSColor = NSCalibratedRGBColorSpace 0.878431 0.0980392 0.160784 1; >>> NSFont = "LucidaGrande 9.00 pt. P [] (0x50bbd0) fobj=0x50b7b0, spc=2.85"; >>> }A hopper is a device shaped like a large funnel, in which substances such >>> as grain, coal, animal food, or sand can be stored.{ >>> CdctPhraseType = Transition; >>> NSColor = NSCalibratedRGBColorSpace 0.227451 0.54902 0.258824 1; >>> NSFont = "LucidaGrande 9.00 pt. P [] (0x50bbd0) fobj=0x50b7b0, spc=2.85"; >>> } >>> { >>> }F.ex: Large trailers came along and tipped it into a big hopper.{ >>> CdctPhraseType = Transition; >>> NSColor = NSCalibratedRGBColorSpace 0.227451 0.54902 0.258824 1; >>> NSFont = "LucidaGrande 9.00 pt. P [] (0x50bbd0) fobj=0x50b7b0, spc=2.85"; >>> } >>> >>> The resulting Rect is too short by about 9 pixels. Anyone had the same >>> problem? >>> >>> Tony Pollard >>> >>> >>> >>> >>> _______________________________________________ >>> >>> Cocoa-dev mailing list ([email protected]) >>> >>> 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: >>> https://lists.apple.com/mailman/options/cocoa-dev/tim%40timschroeder.net >>> >>> This email sent to [email protected] >> > > > _______________________________________________ > > Cocoa-dev mailing list ([email protected]) > > 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: > https://lists.apple.com/mailman/options/cocoa-dev/tonyp%40neutral.co.uk > > This email sent to [email protected] _______________________________________________ Cocoa-dev mailing list ([email protected]) 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: https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to [email protected]
