Last active
March 23, 2018 06:07
-
-
Save Ckitakishi/d0e784b9f0d1f4675efbc3a9918e09cb to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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