Back to index

lightning-sunbird  0.9+nobinonly
Public Member Functions
NSMutableString Class Reference

List of all members.

Public Member Functions

(void- truncateTo:at: [implementation]
(void- truncateToWidth:at:withAttributes: [implementation]

Detailed Description

Definition at line 160 of file NSString+Utils.mm.


Member Function Documentation

- (void) truncateTo: (unsigned)  maxCharacters
at: (ETruncationType truncationType 
[implementation]

Definition at line 162 of file NSString+Utils.mm.

                  :(unsigned)maxCharacters at:(ETruncationType)truncationType
{
  if ([self length] > maxCharacters)
  {
  NSRange replaceRange;
  replaceRange.length = [self length] - maxCharacters;

  switch (truncationType)
  {
    case kTruncateAtStart:
      replaceRange.location = 0;
      break;

    case kTruncateAtMiddle:
      replaceRange.location = maxCharacters / 2;
      break;

    case kTruncateAtEnd:
      replaceRange.location = maxCharacters;
      break;

    default:
#if DEBUG
      NSLog(@"Unknown truncation type in stringByTruncatingTo::");
#endif          
      replaceRange.location = maxCharacters;
      break;
  }
  
  [self replaceCharactersInRange:replaceRange withString:[NSString ellipsisString]];
  }
}

Here is the call graph for this function:

Here is the caller graph for this function:

- (void) truncateToWidth: (float)  maxWidth
at: (ETruncationType truncationType
withAttributes: (NSDictionary *)  attributes 
[implementation]

Definition at line 196 of file NSString+Utils.mm.

                       :(float)maxWidth at:(ETruncationType)truncationType withAttributes:(NSDictionary *)attributes
{
  // First check if we have to truncate at all.
  if ([self sizeWithAttributes:attributes].width > maxWidth)
  {
    // Essentially, we perform a binary search on the string length
    // which fits best into maxWidth.

    float width;
    int lo = 0;
    int hi = [self length];
    int mid;

    // Make a backup copy of the string so that we can restore it if we fail low.
    NSMutableString *backup = [self mutableCopy];
    
    while (hi >= lo)
    {
      mid = (hi + lo) / 2;
      
      // Cut to mid chars and calculate the resulting width
      [self truncateTo:mid at:truncationType];
      width = [self sizeWithAttributes:attributes].width;
      
      if (width > maxWidth) {
        // Fail high - string is still to wide. For the next cut, we can simply
        // work on the already cut string, so we don't restore using the backup. 
        hi = mid - 1;
      } else if (width == maxWidth) {
        // Perfect match, abort the search.
        break;
      } else {
        // Fail low - we cut off to much. Restore the string before cutting again.
        lo = mid + 1;
        [self setString:backup];
      }
    }
    // Perform the final cut (unless this was already a perfect match).
    if (width != maxWidth)
      [self truncateTo:hi at:truncationType];
    [backup release];
  }
}

Here is the call graph for this function:


The documentation for this class was generated from the following file: