Add TUITableViewCell default drawing and options.
Flesh out the class, as well as clean it up.
This commit is contained in:
@@ -18,80 +18,84 @@
|
||||
|
||||
@implementation ExampleTableViewCell
|
||||
|
||||
- (id)initWithStyle:(TUITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
|
||||
{
|
||||
- (id)initWithStyle:(TUITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
|
||||
if((self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])) {
|
||||
textRenderer = [[TUITextRenderer alloc] init];
|
||||
_textRenderer = [[TUITextRenderer alloc] init];
|
||||
_textRenderer.shadowBlur = 1.0f;
|
||||
_textRenderer.verticalAlignment = TUITextVerticalAlignmentMiddle;
|
||||
|
||||
// Add the text renderer to the view so events get routed to it
|
||||
// properly. Text selection, dictionary popup, etc should just work.
|
||||
// You can add more than one.
|
||||
// The text renderer encapsulates an attributed string and a frame.
|
||||
// The attributed string in this case is set by setAttributedString:
|
||||
// which is configured by the table view delegate. The frame needs to
|
||||
// be set before it can be drawn, we do that in drawRect: below.
|
||||
self.textRenderers = [NSArray arrayWithObjects:_textRenderer, nil];
|
||||
|
||||
self.selectionStyle = TUITableViewCellSelectionStyleGray;
|
||||
self.drawingStyle = TUITableViewCellDrawingStyleGradientDown;
|
||||
self.backgroundColor = [NSColor colorWithCalibratedWhite:0.97 alpha:1.0f];
|
||||
self.alternateBackgroundColor = [NSColor colorWithCalibratedWhite:0.92 alpha:1.0f];
|
||||
self.highlightColor = [NSColor colorWithCalibratedWhite:0.87 alpha:1.0f];
|
||||
self.animatesHighlightChanges = YES;
|
||||
|
||||
/*
|
||||
Add the text renderer to the view so events get routed to it properly.
|
||||
Text selection, dictionary popup, etc should just work.
|
||||
You can add more than one.
|
||||
|
||||
The text renderer encapsulates an attributed string and a frame.
|
||||
The attributed string in this case is set by setAttributedString:
|
||||
which is configured by the table view delegate. The frame needs to be
|
||||
set before it can be drawn, we do that in drawRect: below.
|
||||
*/
|
||||
self.textRenderers = [NSArray arrayWithObjects:textRenderer, nil];
|
||||
|
||||
NSTextField *textField = [[NSTextField alloc] initWithFrame:NSMakeRect(20, 180, 91, 22)];
|
||||
[textField.cell setUsesSingleLineMode:YES];
|
||||
[textField.cell setScrollable:YES];
|
||||
|
||||
|
||||
self.textFieldContainer = [[TUIViewNSViewContainer alloc] initWithNSView:textField];
|
||||
self.textFieldContainer.backgroundColor = [NSColor blueColor];
|
||||
[self addSubview:self.textFieldContainer];
|
||||
}
|
||||
|
||||
return self;
|
||||
}
|
||||
|
||||
- (NSAttributedString *)attributedString {
|
||||
return _textRenderer.attributedString;
|
||||
}
|
||||
|
||||
- (void)setAttributedString:(NSAttributedString *)attributedString {
|
||||
_textRenderer.attributedString = attributedString;
|
||||
[self setNeedsDisplay];
|
||||
}
|
||||
|
||||
- (void)layoutSubviews {
|
||||
[super layoutSubviews];
|
||||
|
||||
CGSize textFieldSize = self.textFieldContainer.bounds.size;
|
||||
CGFloat textFieldLeft = CGRectGetWidth(self.bounds) - textFieldSize.width - 16;
|
||||
|
||||
self.textFieldContainer.frame = CGRectMake(textFieldLeft, 14, textFieldSize.width, textFieldSize.height);
|
||||
}
|
||||
|
||||
- (NSAttributedString *)attributedString
|
||||
{
|
||||
return textRenderer.attributedString;
|
||||
}
|
||||
|
||||
- (void)setAttributedString:(NSAttributedString *)attributedString
|
||||
{
|
||||
textRenderer.attributedString = attributedString;
|
||||
[self setNeedsDisplay];
|
||||
}
|
||||
|
||||
- (void)drawRect:(CGRect)rect
|
||||
{
|
||||
CGRect b = self.bounds;
|
||||
CGContextRef ctx = TUIGraphicsGetCurrentContext();
|
||||
- (void)drawRect:(CGRect)rect {
|
||||
[super drawRect:rect];
|
||||
|
||||
if(self.selected) {
|
||||
// selected background
|
||||
CGContextSetRGBFillColor(ctx, .87, .87, .87, 1);
|
||||
CGContextFillRect(ctx, b);
|
||||
} else {
|
||||
// light gray background
|
||||
CGContextSetRGBFillColor(ctx, .97, .97, .97, 1);
|
||||
CGContextFillRect(ctx, b);
|
||||
// Change the text color when state changes, so it looks alright.
|
||||
if(self.highlighted || self.selected) {
|
||||
TUIAttributedString *string = (TUIAttributedString *)_textRenderer.attributedString;
|
||||
[string setColor:[NSColor whiteColor]];
|
||||
_textRenderer.attributedString = string;
|
||||
|
||||
// emboss
|
||||
CGContextSetRGBFillColor(ctx, 1, 1, 1, 0.9); // light at the top
|
||||
CGContextFillRect(ctx, CGRectMake(0, b.size.height-1, b.size.width, 1));
|
||||
CGContextSetRGBFillColor(ctx, 0, 0, 0, 0.08); // dark at the bottom
|
||||
CGContextFillRect(ctx, CGRectMake(0, 0, b.size.width, 1));
|
||||
_textRenderer.shadowColor = [NSColor blackColor];
|
||||
_textRenderer.shadowOffset = CGSizeMake(0, -1);
|
||||
} else {
|
||||
TUIAttributedString *string = (TUIAttributedString *)_textRenderer.attributedString;
|
||||
[string setColor:[NSColor blackColor]];
|
||||
_textRenderer.attributedString = string;
|
||||
|
||||
_textRenderer.shadowColor = [NSColor whiteColor];
|
||||
_textRenderer.shadowOffset = CGSizeMake(0, 1);
|
||||
}
|
||||
|
||||
// text
|
||||
CGRect textRect = CGRectOffset(b, 15, -15);
|
||||
textRenderer.frame = textRect; // set the frame so it knows where to draw itself
|
||||
[textRenderer draw];
|
||||
// Set the frame so it knows where to draw itself.
|
||||
CGRect textRect = self.bounds;
|
||||
textRect.origin.x += self.indentationWidth;
|
||||
textRect.size.width -= (self.textFieldContainer.frame.size.width + 16) + (self.indentationWidth * 2);
|
||||
|
||||
_textRenderer.frame = textRect;
|
||||
[_textRenderer draw];
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
Reference in New Issue
Block a user