Oto wdrożenie podklasy UILabel który naśladuje tła programowo:
UITableViewStandardHeaderLabel.h
#import <UIKit/UIKit.h>
@interface UITableViewStandardHeaderLabel : UILabel
@property (nonatomic) CGFloat topInset;
@property (nonatomic) CGFloat leftInset;
@property (nonatomic) CGFloat bottomInset;
@property (nonatomic) CGFloat rightInset;
@end
UITableViewStandardHeaderLabel.m:
/*!
* @class UITableViewStandardHeaderLabel
* @brief Reimplementation of the UILabel used for a standard UITableView's group headers for customization purposes
*/
@implementation UITableViewStandardHeaderLabel
@synthesize topInset, leftInset, bottomInset, rightInset;
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
self.backgroundColor = [UIColor clearColor];
}
return self;
}
- (void)drawTextInRect:(CGRect)rect
{
UIEdgeInsets insets = {self.topInset, self.leftInset, self.bottomInset, self.rightInset};
return [super drawTextInRect:UIEdgeInsetsInsetRect(rect, insets)];
}
- (void)drawRect:(CGRect)rect
{
CGContextRef context = UIGraphicsGetCurrentContext();
CGGradientRef backgroundGradient;
CGColorSpaceRef rgbColorspace;
size_t num_locations = 2;
CGFloat locations[2] = { 0.0f, 1.0f };
CGFloat components[8] = { 144.0f/255.0f, 159.0f/255.0f, 171.0f/255.0f, 1.0f,
/* start */ 183.0f/255.0f, 192.0f/255.0f, 200.0f/255.0f, 1.0f /* end */ };
rgbColorspace = CGColorSpaceCreateDeviceRGB();
backgroundGradient = CGGradientCreateWithColorComponents(rgbColorspace, components, locations, num_locations);
CGRect currentBounds = self.bounds;
CGPoint topCenter = CGPointMake(CGRectGetMidX(currentBounds), CGRectGetMinY(currentBounds));
CGPoint bottomCenter = CGPointMake(CGRectGetMidX(currentBounds), CGRectGetMaxY(currentBounds));
CGContextDrawLinearGradient(context, backgroundGradient, topCenter, bottomCenter, 0);
UIColor *topBorderLineColor = [UIColor colorWithRed:113.0f/255.0f green:125.0f/255.0f blue:133.0f/255.0f alpha:1.0];
UIColor *secondLineColor = [UIColor colorWithRed:165.0f/255.0f green:177.0f/255.0f blue:187.0f/255.0f alpha:1.0];
UIColor *bottomBorderLineColor = [UIColor colorWithRed:151.0f/255.0f green:157.0f/255.0f blue:164.0f/255.0f alpha:1.0];
[topBorderLineColor setFill];
CGContextFillRect(context, CGRectMake(0, 0, CGRectGetMaxX(currentBounds), 1));
[bottomBorderLineColor setFill];
CGContextFillRect(context, CGRectMake(0, CGRectGetMaxY(currentBounds)-1, CGRectGetMaxX(currentBounds), 1));
[secondLineColor setFill];
CGContextFillRect(context, CGRectMake(0, 1, CGRectGetMaxX(currentBounds), 1));
[super drawRect:rect];
}
@end
Wiem, jak zaimplementować własną komórkę nagłówka, która jest całkiem prosta. Chciałbym jednak dokładnie naśladować wygląd domyślnego stylu nagłówka sekcji dla zwykłego UITableView dostarczonego przez firmę Apple. Zaktualizuję moje pytanie, aby to wyjaśnić. – Bringo
Jeśli chcesz dokładnie to naśladować, nie jestem pewien, ponieważ są to obiekty UIView, a nie specjalna klasa. –
Czy obejrzałeś ten nieformalny protokół: http://developer.apple.com/library/ios/#documentation/uikit/reference/UIAccessibility_Protocol/Introduction/Introduction.html –