Ich weiß, dass dies bereits beantwortet wurde, aber ich hatte Probleme, die Linie über den Gradienten zu ziehen. Ich dachte, es könnte andere geben, die das gleiche Problem haben, also hier ist, wie ich es gelöst habe. Dies funktioniert für iOS 4x
CustomTableViewSectionHeaderWithLine.h
#import <UIKit/UIKit.h>
#import <QuartzCore/QuartzCore.h>
@interface CustomTableViewSectionHeaderWithLine : UIView {
UIColor *topColor;
UIColor *bottomColor;
UIColor *lineColor;
}
@property(nonatomic, retain) UIColor *topColor, *bottomColor, *lineColor;
@end
CustomTableViewSectionHeaderWithLine.m
#import "CustomTableViewSectionHeaderWithLine.h"
@implementation CustomTableViewSectionHeaderWithLine
@synthesize topColor;
@synthesize bottomColor;
@synthesize lineColor;
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Set the colors to something heinous so if you forget you can know immediately
if ([self topColor] == nil) topColor = [UIColor greenColor];
if ([self bottomColor] == nil) bottomColor = [UIColor yellowColor];
if ([self lineColor] == nil) lineColor = [UIColor redColor];
}
return self;
}
- (void)drawRect:(CGRect)rect
{
//[super drawRect:rect];
//add a gradient:
CAGradientLayer *gradientLayer = [[[CAGradientLayer alloc] init] autorelease];
[gradientLayer setBounds:[self bounds]];
CGRect newRect = CGRectMake(rect.origin.x, rect.origin.y, rect.size.width, rect.size.height -1);
[gradientLayer setFrame:newRect];
[gradientLayer setColors:[NSArray arrayWithObjects:(id)[topColor CGColor], (id)[bottomColor CGColor], nil]];
[[self layer] insertSublayer:gradientLayer atIndex:0];
//draw line
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextBeginPath(ctx);
// This gets the RGB Float values from the color initialized for lineColor
const float* colors = CGColorGetComponents(lineColor.CGColor);
CGContextSetRGBStrokeColor(ctx, colors[0], colors[1], colors[2], 1);
//CGContextSetGrayStrokeColor(ctx, 0, 1);
CGContextMoveToPoint(ctx, 0, rect.size.height);
CGContextAddLineToPoint(ctx, rect.size.width, rect.size.height);
CGContextStrokePath(ctx);
}
- (void)dealloc
{
[super dealloc];
}
@end
Usage (Implementieren Sie diese beiden Methoden in Ihrem TableViewDelegate
- (UIView *) tableView:(UITableView *) tableView viewForHeaderInSection:(NSInteger)section {
CustomTableViewSectionHeaderWithLine *customView = [[[CustomTableViewSectionHeaderWithLine alloc] initWithFrame:CGRectMake(0.0, 0.0, 360.0, 25.0)] autorelease];
[customView setTopColor:[UIColor whiteColor]];
[customView setBottomColor:[UIColor blackColor]];
[customView setLineColor:[UIColor whiteColor]];
UILabel *label = [[[UILabel alloc] initWithFrame:CGRectMake(10, 3, tableView.bounds.size.width - 10, 18)] autorelease];
label.text = [tableView.dataSource tableView:tableView titleForHeaderInSection:section];
label.textColor = [UIColor blackColor];
label.backgroundColor = [UIColor clearColor];
[customView addSubview:label];
return customView;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
return 25;
}
Romain finden Sie Änderungen oben Ich habe einige kleine Farbänderungen an Ihrem Code vorgenommen Gradient ist außerhalb des Header-Bereich? und ich bin nicht in der Lage, die Linie weiß zu bekommen. Ich habe eine Beschriftung mit der Höhe 1 hinzugefügt, die das letzte Bild ist. Daher weiß ich, dass die graue Linie keine Höhe 1 ist. – Jules
CoreGraphics wird immer über Pixelgrenzen hinweg gezeichnet, was bedeutet, dass wenn Sie eine Pixellinie haben, diese zwischen zwei Pixel gezogen wird , so dass es als klobige, grobe 2-Pixel-Linie erscheint, die Farbe ausgespült hat. Für Ihre horizontale Linie müssen Sie die Y-Koordinate um 0,5 und umgekehrt für vertikale Linien verschieben. –
Ich musste eine Korrektur vornehmen '[gradientLayer setFrame: rect];' und nachdem ich meine untere Zeile mit CrystalSkulls fixiert hatte, war meine Zeile hinter dem Farbverlauf versteckt. Also blieb ich bei meinem One-Pixel-Label. Alles sieht ziemlich gut aus :) – Jules