-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPDButtonTextOnImageCell.m
127 lines (77 loc) · 2.66 KB
/
PDButtonTextOnImageCell.m
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
//
// PDButtonTextOnImageCell.m
// SproutedInterface
//
// Created by Philip Dow on 1/3/06.
// Copyright Sprouted. All rights reserved.
// Inquiries should be directed to [email protected]
//
#import <SproutedInterface/PDButtonTextOnImageCell.h>
@implementation PDButtonTextOnImageCell
- (id)initWithCoder:(NSCoder *)decoder {
if ( self = [super initWithCoder:decoder] ) {
[self setBordered:NO];
}
return self;
}
- (id)initTextCell:(NSString *)aString {
if ( self = [super initTextCell:aString] ) {
[self setBordered:NO];
}
return self;
}
- (void) dealloc {
[super dealloc];
}
#pragma mark -
- (NSRect)drawingRectForBounds:(NSRect)theRect {
return theRect;
}
- (NSRect)titleRectForBounds:(NSRect)theRect {
return theRect;
}
- (void)drawWithFrame:(NSRect)cellFrame inView:(NSView *)controlView {
//
// bypass the frame
//
[self drawInteriorWithFrame:cellFrame inView:controlView];
}
- (void)drawInteriorWithFrame:(NSRect)cellFrame inView:(NSView *)controlView {
float height = cellFrame.size.height;
float width = cellFrame.size.width;
float alpha = 1.0;
NSSize centerSize;
NSSize textSize;
NSRect targetRect;
NSRect textRect;
NSImage *cellImage = [self image];
NSMutableAttributedString *attrValue = [[[NSMutableAttributedString alloc] initWithString:[self title]attributes:
[NSDictionary dictionaryWithObjectsAndKeys:[self font], NSFontAttributeName, nil]] autorelease];
centerSize = [cellImage size];
targetRect = NSMakeRect(width/2-centerSize.width/2, height/2-centerSize.height/2, centerSize.width, centerSize.height);
if ( [self isHighlighted] ) {
[[NSColor blackColor] set];
NSRectFillUsingOperation(cellFrame, NSCompositeSourceOver);
alpha = 0.6;
}
[cellImage setFlipped:YES];
[cellImage drawInRect:targetRect
fromRect:NSMakeRect(0,0,centerSize.width,centerSize.height)
operation:NSCompositeSourceOver fraction:alpha];
textSize = [attrValue size];
// a maximum rectangle for the text
NSRect maxTextRect = NSMakeRect(4,height/2-textSize.height/2 + 1, width-8, textSize.height);
// the required size
textRect = NSMakeRect(width/2-textSize.width/2, height/2-textSize.height/2 + 1, textSize.width, textSize.height);
if ( textRect.size.width > maxTextRect.size.width ) {
NSMutableParagraphStyle *parStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopyWithZone:[self zone]];
[parStyle setLineBreakMode:NSLineBreakByTruncatingTail];
[attrValue addAttribute:NSParagraphStyleAttributeName value:parStyle range:NSMakeRange(0,[attrValue length])];
[attrValue drawInRect:maxTextRect];
[parStyle release];
}
else {
[attrValue drawInRect:textRect];
}
}
@end