ボタンまたはビューをサブクラス化し、init メソッドと drawRect メソッドをオーバーライドします。これは、私がサブクラス化した UIView の例です。
#import "CustomSectionView.h"
#import <QuartzCore/QuartzCore.h>
@interface CustomSectionView () {
UIColor *headerColor;
UIColor *linesColor;
}
@end
@implementation CustomSectionView
@synthesize titleLabel;
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
headerColor = [UIColor whiteColor];
linesColor = [UIColor whiteColor];
self.layer.cornerRadius = 6;
self.layer.masksToBounds = YES;
self.backgroundColor = [UIColor whiteColor];
//title label
titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(16, 3, 110, 18)];
titleLabel.backgroundColor = [UIColor clearColor];
titleLabel.textAlignment = NSTextAlignmentLeft;
titleLabel.font = [UIFont fontWithName:@"GillSans-Bold" size:15];
[self addSubview:titleLabel];
}
return self;
}
- (id)initWithFrame:(CGRect)frame headerColor:(UIColor *)color linesColor:(UIColor *)color1
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
if (color == nil) {
color = [UIColor whiteColor];
}
headerColor = color;
if (color1 == nil) {
color1 = [UIColor clearColor];
}
linesColor = color1;
self.layer.cornerRadius = 6;
self.layer.masksToBounds = YES;
self.backgroundColor = [UIColor whiteColor];
//title label
titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(16, 3, 110, 18)];
titleLabel.backgroundColor = [UIColor clearColor];
titleLabel.textAlignment = NSTextAlignmentLeft;
titleLabel.font = [UIFont fontWithName:@"GillSans-Bold" size:15];
[self addSubview:titleLabel];
}
return self;
}
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
// Drawing code
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(context, 2.0);
CGContextSetStrokeColorWithColor(context, linesColor.CGColor);
CGContextMoveToPoint(context, 0, 0);
CGContextAddLineToPoint(context, 0, self.frame.size.height);
CGContextStrokePath(context);
CGContextSetLineWidth(context, 2.0);
CGContextSetStrokeColorWithColor(context, linesColor.CGColor);
CGContextMoveToPoint(context, self.frame.size.width, 0);
CGContextAddLineToPoint(context, self.frame.size.width, self.frame.size.height);
CGContextStrokePath(context);
CGRect rectangle = CGRectMake(0,0,self.frame.size.width,25);
CGContextSetFillColorWithColor(context, headerColor.CGColor);
CGContextFillRect(context, rectangle);
}
@end