Dưới đây là một thực hiện của một lớp con UILabel mà bắt chước nền programatically:
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
Tôi biết cách triển khai ô tiêu đề của chính mình khá thẳng về phía trước. Tuy nhiên tôi muốn bắt chước chính xác giao diện của tiêu đề phần mặc định cho UITableView đơn giản như được cung cấp bởi Apple. Tôi sẽ cập nhật câu hỏi của mình để làm rõ điều này. – Bringo
Nếu bạn muốn bắt chước chính xác, tôi không chắc chắn, vì chúng là các đối tượng UIView và không phải là một lớp đặc biệt của riêng chúng. –
Bạn đã xem giao thức không chính thức này chưa: http://developer.apple.com/library/ios/#documentation/uikit/reference/UIAccessibility_Protocol/Introduction/Introduction.html –