Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 90 lines (76 sloc) 3.335 kb
dc5be1c Initial check-in.
uli authored
1 //
2 // NSString+TruncatedForWidthWithAttributesMode.m
3 // xTableImage
4 //
5 // Created by Uli Kusterer on 28.09.06.
720b0e7 @uliwitness Added licensing information to the source files.
authored
6 // Copyright 2006 Uli Kusterer.
7 //
8 // This software is provided 'as-is', without any express or implied
9 // warranty. In no event will the authors be held liable for any damages
10 // arising from the use of this software.
11 //
12 // Permission is granted to anyone to use this software for any purpose,
13 // including commercial applications, and to alter it and redistribute it
14 // freely, subject to the following restrictions:
15 //
16 // 1. The origin of this software must not be misrepresented; you must not
17 // claim that you wrote the original software. If you use this software
18 // in a product, an acknowledgment in the product documentation would be
19 // appreciated but is not required.
20 //
21 // 2. Altered source versions must be plainly marked as such, and must not be
22 // misrepresented as being the original software.
23 //
24 // 3. This notice may not be removed or altered from any source
25 // distribution.
dc5be1c Initial check-in.
uli authored
26 //
27
28 #import "NSString+TruncatedForWidthWithAttributesMode.h"
29
30
31 @implementation NSString (UKTruncatedForWidthWithAttributesMode)
32
33 // -----------------------------------------------------------------------------
34 // Returns a truncated version of the specified string that fits a width:
35 // Appends/Inserts three periods as an "ellipsis" to/in the string to
36 // indicate when and where it was truncated.
37 // -----------------------------------------------------------------------------
38
39 -(NSString*) truncatedForWidth: (int)wid withAttributes: (NSDictionary*)attrs
40 mode: (NSLineBreakMode)truncateMode
41 {
42 NSSize txSize = [self sizeWithAttributes: attrs];
43
44 if( txSize.width <= wid ) // Don't do anything if it fits.
45 return self;
46
47 NSMutableString* currString = [NSMutableString string];
48 NSRange rangeToCut = { 0, 0 };
49
50 if( truncateMode == NSLineBreakByTruncatingTail )
51 {
52 rangeToCut.location = [self length] -1;
53 rangeToCut.length = 1;
54 }
55 else if( truncateMode == NSLineBreakByTruncatingHead )
56 {
57 rangeToCut.location = 0;
58 rangeToCut.length = 1;
59 }
60 else // NSLineBreakByTruncatingMiddle
61 {
62 rangeToCut.location = [self length] / 2;
63 rangeToCut.length = 1;
64 }
65
66 while( txSize.width > wid )
67 {
68 if( truncateMode != NSLineBreakByTruncatingHead && rangeToCut.location <= 1 )
69 return @"...";
70
71 [currString setString: self];
72 [currString replaceCharactersInRange: rangeToCut withString: @"..."];
73 txSize = [currString sizeWithAttributes: attrs];
74 rangeToCut.length++;
75 if( truncateMode == NSLineBreakByTruncatingHead )
76 ; // No need to fix location, stays at start.
77 else if( truncateMode == NSLineBreakByTruncatingTail )
78 rangeToCut.location--; // Fix location so range that's one longer still lies inside our string at end.
79 else if( (rangeToCut.length & 1) != 1 ) // even? NSLineBreakByTruncatingMiddle
80 rangeToCut.location--; // Move location left every other time, so it grows to right and left and stays centered.
81
82 if( rangeToCut.location < 0 || (rangeToCut.location +rangeToCut.length) > [self length] )
83 return @"...";
84 }
85
86 return currString;
87 }
88
89 @end
Something went wrong with that request. Please try again.