• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Measuring length of string compared to text field
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Measuring length of string compared to text field


  • Subject: Re: Measuring length of string compared to text field
  • From: m <email@hidden>
  • Date: Fri, 2 Apr 2004 01:47:01 -0800

make this a category methdod for NSString, no warranty express or implied, blah blah blah.

- (NSString*) truncatedStringForWidth:(double) inWidth andAttributes:(NSDictionary*) inAttributes
{
unichar ellipsesCharacter = 0x2026;
NSString* ellipsisString = [NSString stringWithCharacters:&ellipsesCharacter length:1];

NSString* truncatedString = [NSString stringWithString:self];
int truncatedStringLength = [truncatedString length];

if ((truncatedStringLength > 2) && ([truncatedString sizeWithAttributes:inAttributes].width > inWidth))
{
double targetWidth = inWidth - [ellipsisString sizeWithAttributes:inAttributes].width;
NSCharacterSet* whiteSpaceCharacters = [NSCharacterSet whitespaceAndNewlineCharacterSet];

while([truncatedString sizeWithAttributes:inAttributes].width > targetWidth && truncatedStringLength)
{
truncatedStringLength--;
while ([whiteSpaceCharacters characterIsMember:[truncatedString characterAtIndex:(truncatedStringLength -1)]])
{
// never truncate immediately after whitespace
truncatedStringLength--;
}

truncatedString = [truncatedString substringToIndex:truncatedStringLength];
}

truncatedString = [truncatedString stringByAppendingString:ellipsisString];
}

return truncatedString;
}


On Apr 1, 2004, at 11:57 PM, Chad Armstrong wrote:

Is there a way to tell if a string is too long for a text field within the code? I'm trying for an effect similar to when one empties the trash which contains a file with a long file name, and the file name has an ellipsis (...) at the end to indicate there is more to the name.

I first typed in a bunch of numbers (123456789012345...) to try and judge the length of the field. However, unless a fixed width font is used (like Courier, I believe), this length of how many characters can visibly fit into a text field will change, depending on the letter and if it is capitalized or not.

Is there a solution to my problem? I'm currently using the Lucida Grande font.

Chad Armstrong
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.


References: 
 >Measuring length of string compared to text field (From: Chad Armstrong <email@hidden>)

  • Prev by Date: Re: BEST BOOK FOR LEARNING COCOA
  • Next by Date: Re:Altering attributes of hyperlinked text
  • Previous by thread: Re: Measuring length of string compared to text field
  • Next by thread: Rotating view whilst printing
  • Index(es):
    • Date
    • Thread