Revert TUITableViewCell changes

This reverts commit f5ade96e06, reversing changes made to 91d97d6c62.

They were causing too much breakage for us.
This commit is contained in:
Justin Spahr-Summers
2012-11-12 10:31:28 -08:00
parent 7b76c240a1
commit fd1bbba8f8
9 changed files with 178 additions and 555 deletions
+54 -62
View File
@@ -18,88 +18,80 @@
@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];
// Set up the alternating row "look". Very slick, clean look. Just by
// setting up a few colors (and optionally a few styles, and if we
// need more, overridable methods) we can achieve whatever look we want.
self.backgroundColor = [NSColor colorWithCalibratedWhite:0.97 alpha:1.0f];
self.highlightColor = [NSColor colorWithCalibratedWhite:0.87 alpha:1.0f];
self.selectionColor = [NSColor colorWithCalibratedWhite:0.77 alpha:1.0f];
self.alternateBackgroundColor = [NSColor colorWithCalibratedWhite:0.92 alpha:1.0f];
self.alternateHighlightColor = [NSColor colorWithCalibratedWhite:0.82 alpha:1.0f];
self.alternateSelectionColor = [NSColor colorWithCalibratedWhite:0.72 alpha:1.0f];
// Instead of using a TUILabel or TUITextField, let's take a little
// course in the art of using a TUITextRenderer. It acts as a simple
// text -> layer rendering mechanism class with tie-ins to TUIView,
// but can achieve far more if used right (like TUITextView).
_textRenderer = [[TUITextRenderer alloc] init];
_textRenderer.verticalAlignment = TUITextVerticalAlignmentMiddle;
_textRenderer.shouldRefuseFirstResponder = YES;
_textRenderer.shadowBlur = 1.0f;
_textRenderer.shadowColor = [NSColor whiteColor];
_textRenderer.shadowOffset = CGSizeMake(0, 1);
// 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 = @[_textRenderer];
// Add in a standard Cocoa text field. We have to enclose this within
// a TUIViewNSViewContainer, and we MUST adjust only the frame of that
// container, and not the text field's itself.
/*
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;
}
// Create a simple attributed string setter/getter to the text renderer.
- (NSAttributedString *)attributedString {
return _textRenderer.attributedString;
}
- (void)setAttributedString:(NSAttributedString *)attributedString {
_textRenderer.attributedString = attributedString;
[self setNeedsDisplay];
}
// When we subclass any view and override the layout or drawing methods,
// it's a must to insert a super call before doing your own thing.
- (void)layoutSubviews {
[super layoutSubviews];
// Set the text field container's frame (NOT THE TEXT FIELD).
CGSize textFieldSize = self.textFieldContainer.bounds.size;
CGFloat textFieldLeft = CGRectGetWidth(self.bounds) - textFieldSize.width - 16;
self.textFieldContainer.frame = CGRectMake(textFieldLeft, 14, textFieldSize.width, textFieldSize.height);
// Set the text renderer's frame. Take indentation into account.
CGFloat indentation = 10.0f;
CGRect textRect = self.bounds;
textRect.origin.x += indentation;
textRect.size.width -= (self.textFieldContainer.frame.size.width + 16) + (indentation * 2);
_textRenderer.frame = textRect;
}
// Note that we only override the drawRect: to add in a call to
// let the text renderer draw as well. For TUITableView, customization
// is preferrably left to the overridable drawing methods - not this.
- (void)drawRect:(CGRect)rect {
[super drawRect:rect];
[_textRenderer draw];
- (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();
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);
// 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));
}
// text
CGRect textRect = CGRectOffset(b, 15, -15);
textRenderer.frame = textRect; // set the frame so it knows where to draw itself
[textRenderer draw];
}
@end