1 | //␊ |
2 | // CenterTextField.mm␊ |
3 | // ChameleonPrefPane␊ |
4 | //␊ |
5 | // Created by Rekursor on 11/10/11.␊ |
6 | //␊ |
7 | ␊ |
8 | #import "CenterTextFieldCell.h"␊ |
9 | ␊ |
10 | //--------------------------------------------------------------------------␊ |
11 | @implementation CenteredTextFieldCell␊ |
12 | //--------------------------------------------------------------------------␊ |
13 | - (void)drawInteriorWithFrame:(NSRect)cellFrame inView:(NSView *)controlView␊ |
14 | {␊ |
15 | NSSize contentSize = [self cellSize];␊ |
16 | cellFrame.origin.y += (cellFrame.size.height - contentSize.height) / 2.0;␊ |
17 | cellFrame.size.height = contentSize.height;␊ |
18 | ␉␊ |
19 | ␉NSAttributedString *drawString = [self attributedStringValue];␊ |
20 | ␉␊ |
21 | ␉NSRange range;␊ |
22 | ␉NSDictionary *attributes = [drawString attributesAtIndex:0 effectiveRange:&range];␊ |
23 | ␉␊ |
24 | ␉float maxWidth = cellFrame.size.width;␊ |
25 | ␉float stringWidth = [drawString size].width;␊ |
26 | ␉␊ |
27 | ␉if (maxWidth < stringWidth)␊ |
28 | ␉{␊ |
29 | ␉␉int i;␊ |
30 | ␉␉␊ |
31 | ␉␉for (i = 0;i <= [drawString length];i++)␊ |
32 | ␉␉{␊ |
33 | ␉␉␉if ([[drawString attributedSubstringFromRange:NSMakeRange(0,i)]size].width >= maxWidth)␊ |
34 | ␉␉␉{␉␊ |
35 | ␉␉␉␉drawString = [[NSMutableAttributedString alloc] initWithString:␊ |
36 | ␉␉␉␉␉␉␉ [[[drawString attributedSubstringFromRange:NSMakeRange(0,i-3)]string] stringByAppendingString:@"..."]attributes:attributes];␊ |
37 | ␉␉␉␉[drawString autorelease];␊ |
38 | ␉␉␉}␊ |
39 | ␉␉}␊ |
40 | ␉}␊ |
41 | ␉␊ |
42 | ␉[drawString drawInRect:cellFrame];␊ |
43 | ␉␊ |
44 | }␊ |
45 | ␊ |
46 | @end␊ |
47 | ␊ |
48 | ␊ |
49 | |