Skip to content

Instantly share code, notes, and snippets.

@Ckitakishi
Last active March 23, 2018 06:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Ckitakishi/d0e784b9f0d1f4675efbc3a9918e09cb to your computer and use it in GitHub Desktop.
Save Ckitakishi/d0e784b9f0d1f4675efbc3a9918e09cb to your computer and use it in GitHub Desktop.
// Source:
@implementation UIView (Border)
- (void)addBorder:(UIRectEdge)edge color:(UIColor *)color thickness:(CGFloat)thickness
{
if ((UIRectEdgeTop & edge) == UIRectEdgeTop) {
[self.layer addSublayer:[self borderLayerMake:CGRectMake(0, 0, CGRectGetWidth(self.frame), thickness) color:color]];
}
if ((UIRectEdgeBottom & edge) == UIRectEdgeBottom) {
[self.layer addSublayer:[self borderLayerMake:CGRectMake(0, CGRectGetHeight(self.frame) - thickness, CGRectGetWidth(self.frame), thickness) color:color]];
}
if ((UIRectEdgeLeft & edge) == UIRectEdgeLeft) {
[self.layer addSublayer:[self borderLayerMake:CGRectMake(0, 0, thickness, CGRectGetHeight(self.frame)) color:color]];
}
if ((UIRectEdgeRight & edge) == UIRectEdgeRight) {
[self.layer addSublayer:[self borderLayerMake:CGRectMake(CGRectGetWidth(self.frame) - thickness, 0, thickness, CGRectGetHeight(self.frame)) color:color]];
}
}
- (CALayer *)borderLayerMake:(CGRect)rect color:(UIColor *)color {
CALayer *border = [CALayer layer];
border.backgroundColor = color.CGColor;
border.frame = rect;
return border;
}
@end
// Sample:
@implementation XXView
[self.label addBorder:UIRectEdgeTop | UIRectEdgeBottom color:[UIColor lightGrayColor] thickness:1.0f / [UIScreen mainScreen].scale];
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment