// 枠強調オプション
typedef NS_OPTIONS(NSUInteger, eBorderEmphasisOption)
{
BorderEmphasisOption_None = 0,
BorderEmphasisOption_Top = 1 << 0,
BorderEmphasisOption_Bottom = 1 << 1,
BorderEmphasisOption_Left = 1 << 2,
BorderEmphasisOption_Right = 1 << 3,
BorderEmphasisOption_All = NSUIntegerMax
};
@interface BorderExView : UIView
@property (nonatomic) eBorderEmphasisOption borderEmphasis;
@property (nonatomic, strong) UIColor* borderColor;
@property (nonatomic) float borderWidth;
@end
@interface BorderExView ()
{
eBorderEmphasisOption _borderEmphasis;
}
@end
@implementation BorderExView
@synthesize borderColor = _borderColor;
@synthesize borderWidth = _borderWidth;
@dynamic borderEmphasis;
- (eBorderEmphasisOption)borderEmphasis
{
return _borderEmphasis;
}
- (void)setBorderEmphasis:(eBorderEmphasisOption)borderEmphasis
{
_borderEmphasis = borderEmphasis;
[self setNeedsDisplay];
}
- (id)initWithFrame:(CGRect)frame
{
if (self = [super initWithFrame:frame]) {
_borderEmphasis = BorderEmphasisOption_None;
self.borderColor = [UIColor blackColor];
self.borderWidth = 1.0;
}
return self;
}
- (void)drawRect:(CGRect)rect
{
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(ctx, self.borderWidth);
CGContextSetStrokeColorWithColor(ctx, self.borderColor.CGColor);
if (self.borderEmphasis & BorderEmphasisOption_Top) {
CGContextMoveToPoint(ctx, 0, 0);
CGContextAddLineToPoint(ctx, self.frame.size.width, 0);
CGContextStrokePath(ctx);
}
if (self.borderEmphasis & BorderEmphasisOption_Bottom) {
CGContextMoveToPoint(ctx, 0, self.frame.size.height);
CGContextAddLineToPoint(ctx, self.frame.size.width, self.frame.size.height);
CGContextStrokePath(ctx);
}
if (self.borderEmphasis & BorderEmphasisOption_Left) {
CGContextMoveToPoint(ctx, 0, 0);
CGContextAddLineToPoint(ctx, 0, self.frame.size.height);
CGContextStrokePath(ctx);
}
if (self.borderEmphasis & BorderEmphasisOption_Right) {
CGContextMoveToPoint(ctx, self.frame.size.width, 0);
CGContextAddLineToPoint(ctx, self.frame.size.width, self.frame.size.height);
CGContextStrokePath(ctx);
}
}
@end
BorderExView* view = [[BorderExView alloc] initWithFrame:CGRectMake(50, 50, 100, 100)];
[self.view addSubview:view];
view.backgroundColor = [UIColor yellowColor];
view.borderColor = [UIColor grayColor];
view.borderWidth = 5.0;
view.borderEmphasis = BorderEmphasisOption_Left | BorderEmphasisOption_Top;
コメントする