Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > sizeWithAttributes: maxWidth:...?

sizeWithAttributes: maxWidth:...?
Thread Tools
Professional Poster
Join Date: Dec 2000
Location: Chicago, Illinois
Status: Offline
Reply With Quote
Jun 12, 2004, 11:19 AM
Howdy all. I have a funny feeling this have been covered before, but it's early on a saturday and my data processing skills aren't the best right now I want to find out have big a String will be when I draw with a set of attibutes in an NSCell. sizeWithAttrubutes: works, except for that my NSCell subclass has a maxWidth, so if the String is too big, it begins to wrap. But sizeWithAttributes: doesn't take into account trying to draw the String in a boundede environment like that, so it never returns an NSSize with a height different between a 10 letter word and a 100 letter word.

I'm guesing I can have some fun String parsing ahead of me. Anyone got any code already written or posted somewhere? If not, I'll just get down an dirty with NSString again.

Anyone, this sounds like a good addition for Apple to add to the NSString class.

Matt Fahrenbacher
Ghoser777  (op)
Professional Poster
Join Date: Dec 2000
Location: Chicago, Illinois
Status: Offline
Reply With Quote
Jun 12, 2004, 01:21 PM
Here would be my solution (thought of after my morning run)

It seems to work so far...

- (NSSize)string: (NSString *)msg sizeWithAttributes: (NSDictionary *)dict maxWidth: (int)width { NSMutableString *str = [NSMutableString string]; NSSize strSize; int i; for(i=0; i<[msg length]; i++) { char c = [msg characterAtIndex:i]; [str appendFormat:@"%c",c]; strSize = [str sizeWithAttributes:textAttributes]; if(strSize.width > width) { //our problem depends on a couple of things... //no problem - just remove and re-add new line character if([[NSCharacterSet whitespaceAndNewlineCharacterSet] characterIsMember:c]) [str replaceCharactersInRange:NSMakeRange([str length]-1,1) withString:@"\n"]; else { //we either need to find the first white space on the current line (if applicable) int j; char d = 0; for(j=[str length]-1; j>=0; j--) { d = [str characterAtIndex:j]; if([[NSCharacterSet whitespaceAndNewlineCharacterSet] characterIsMember:d]) break; } if(d == '\n' || j < 0) //no white space found - time to just add a new line { [str replaceCharactersInRange:NSMakeRange([str length]-1,1) withString:@"\n"]; [str appendFormat:@"%c",c]; } else //white space located! { //replace the white space with a new line [str replaceCharactersInRange:NSMakeRange(j,1) withString:@"\n"]; } } } } return strSize; }
Thread Tools
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Privacy Policy
All times are GMT -4. The time now is 07:51 AM.
All contents of these forums © 1995-2017 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.8 © 2000-2017, Jelsoft Enterprises Ltd.,