-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathITMultilineTextFieldCell.m
More file actions
103 lines (75 loc) · 4.84 KB
/
ITMultilineTextFieldCell.m
File metadata and controls
103 lines (75 loc) · 4.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#import "ITMultilineTextFieldCell.h"
@implementation ITMultilineTextFieldCell
- (void)drawInteriorWithFrame:(NSRect)cellFrame inView:(NSView *)controlView {
if ([(id)[self objectValue] isKindOfClass:[NSArray class]]) {
/*
Okay, here's the different possibilities for the objectValue of this cell, and how they're displayed:
NSArray of NSStrings - Draw first line with System Font, following lines with Small System Font
NSArray of NSAttributedStrings - Draw as given
NSArray of both - Draw each line as above!
The number of lines is determined by the contents of the array...
*/
NSColor *defaultColor;
NSMutableParagraphStyle *paragraphStyle = [[[NSParagraphStyle defaultParagraphStyle] mutableCopy] autorelease];
[paragraphStyle setLineBreakMode:NSLineBreakByTruncatingTail];
NSPoint cellPoint = cellFrame.origin;
NSSize cellSize = cellFrame.size;
NSRect secondaryLineRect;
if ([self isHighlighted] && ([self highlightColorWithFrame:cellFrame inView:controlView]!=[NSColor secondarySelectedControlColor])) {
defaultColor = [NSColor whiteColor];
} else {
defaultColor = [NSColor blackColor];
}
// Process the first line...
{
NSDictionary *firstLineAttributes = [NSDictionary dictionaryWithObjectsAndKeys:[NSFont boldSystemFontOfSize:[NSFont systemFontSize]], NSFontAttributeName, defaultColor, NSForegroundColorAttributeName, paragraphStyle, NSParagraphStyleAttributeName, nil];
NSRect firstLineRect = NSMakeRect(cellPoint.x+5, cellPoint.y+1, cellSize.width-5, cellSize.height);
id firstString = [(NSArray *)[self objectValue] objectAtIndex:0];
NSMutableAttributedString *firstAttrString;
if ([firstString isKindOfClass:[NSAttributedString class]]) {
firstAttrString = [[[NSMutableAttributedString alloc] initWithAttributedString:firstString] autorelease];
[firstAttrString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0,[firstAttrString length])];
if ([defaultColor isEqual:[NSColor whiteColor]]) {
[firstAttrString addAttribute:NSForegroundColorAttributeName value:defaultColor range:NSMakeRange(0,[firstAttrString length])];
}
} else if ([firstString isKindOfClass:[NSString class]]) {
firstAttrString = [[[NSMutableAttributedString alloc] initWithString:firstString attributes:firstLineAttributes] autorelease];
} else {
firstAttrString = [[[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"Object (%@) is not a string", firstString] attributes:firstLineAttributes] autorelease];
}
[controlView lockFocus];
[firstAttrString drawInRect:firstLineRect];
[controlView unlockFocus];
secondaryLineRect = NSMakeRect(cellPoint.x+5, (cellPoint.y+1+[firstAttrString size].height), cellSize.width-5, cellSize.height);
}
// Process the secondary lines
{
NSDictionary *secondaryLineAttributes = [NSDictionary dictionaryWithObjectsAndKeys:[NSFont systemFontOfSize:[NSFont smallSystemFontSize]], NSFontAttributeName, defaultColor, NSForegroundColorAttributeName, paragraphStyle, NSParagraphStyleAttributeName, nil];
NSMutableArray *tMArray = [NSMutableArray arrayWithArray:(NSArray *)[self objectValue]];
[tMArray removeObjectAtIndex:0]; // Remove the first line string... already handled that above!
NSEnumerator *enumerator = [tMArray objectEnumerator];
id secondaryString;
while ((secondaryString = [enumerator nextObject])) {
NSMutableAttributedString *secondaryAttrString;
if ([secondaryString isKindOfClass:[NSAttributedString class]]) {
secondaryAttrString = [[[NSMutableAttributedString alloc] initWithAttributedString:secondaryString] autorelease];
[secondaryAttrString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0,[secondaryAttrString length])];
if ([defaultColor isEqual:[NSColor whiteColor]]) {
[secondaryAttrString addAttribute:NSForegroundColorAttributeName value:defaultColor range:NSMakeRange(0,[secondaryAttrString length])];
}
} else if ([secondaryString isKindOfClass:[NSString class]]) {
secondaryAttrString = [[[NSMutableAttributedString alloc] initWithString:secondaryString attributes:secondaryLineAttributes] autorelease];
} else {
secondaryAttrString = [[[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"Object (%@) is not a string", secondaryString] attributes:secondaryLineAttributes] autorelease];
}
[controlView lockFocus];
[secondaryAttrString drawInRect:secondaryLineRect];
[controlView unlockFocus];
secondaryLineRect.origin.y = secondaryLineRect.origin.y+[secondaryAttrString size].height; // modify the rect for the next loop, based on the size and location of the most recently processed line.
}
}
} else {
[super drawInteriorWithFrame:cellFrame inView:controlView];
}
}
@end