Friday, 21 October 2016

CustomeBoarder

//  UIView+CustomBorder.H

#import <UIKit/UIKit.h>

typedef enum : NSUInteger {
    CustomBorderAll = 0,
    CustomBorderTop,
    CustomBorderBottom,
    CustomBorderLeft,
    CustomBorderRight,
} CustomBorderType;
static NSString * const CustomBorderMarginTopAttributeName = @"marginTop" ;
static NSString * const CustomBorderMarginLeftAttributeName = @"marginLeft" ;
static NSString * const CustomBorderMarginRightAttributeName = @"marginRight" ;
static NSString * const CustomBorderMarginBottomAttributeName = @"marginBottom" ;


@interface UIView (CustomBorder)

/**
 *  Draw top border
 *
 *  @param width : Border width
 *  @param color : Border color
 *  @param attrs : Other attributes
 */
- (void)drawTopBorder:(CGFloat)width color:(UIColor *)color attributes:(NSDictionary<NSString *, id> *)attrs;

/**
 *  Draw left border
 *
 *  @param width : Border width
 *  @param color : Border color
 *  @param attrs : Other attributes
 */
- (void)drawLeftBorder:(CGFloat)width color:(UIColor *)color attributes:(NSDictionary<NSString *, id> *)attrs;

/**
 *  Draw right border
 *
 *  @param width : Border width
 *  @param color : Border color
 *  @param attrs : Other attributes
 */
- (void)drawRightBorder:(CGFloat)width color:(UIColor *)color attributes:(NSDictionary<NSString *, id> *)attrs;

/**
 *  Draw bottom border
 *
 *  @param width : Border width
 *  @param color : Border color
 *  @param attrs : Other attributes
 */
- (void)drawBottomBorder:(CGFloat)width color:(UIColor *)color attributes:(NSDictionary<NSString *, id> *)attrs;

/**
 Remove Border

 @param type : CustomBorderType
 */
- (void)removeBorder:(CustomBorderType)type;


@end






//  UIView+CustomBorder.m

 
#import "UIView+CustomBorder.h"
 
@implementation UIView (CustomBorder)
 
static int const CustomBorderTagTop = 878787;
static int const CustomBorderTagBottom = 878788;
static int const CustomBorderTagLeft = 878789;
static int const CustomBorderTagRight = 878790;
 
- (NSMutableDictionary *)getDefaultAttributes{
 
    NSMutableDictionary *metrics = [NSMutableDictionary new];
    
    // Set Default Margin
    [metrics setObject:@0 forKey:CustomBorderMarginTopAttributeName];
    [metrics setObject:@0 forKey:CustomBorderMarginLeftAttributeName];
    [metrics setObject:@0 forKey:CustomBorderMarginRightAttributeName];
    [metrics setObject:@0 forKey:CustomBorderMarginBottomAttributeName];
    
    return metrics;
}
 
/**
 *  Draw top border
 *
 *  @param width : CGFloat
 *  @param color : UIColor
 *  @param attrs : NSDictionary<NSString *, id>
 */
- (void)drawTopBorder:(CGFloat)width color:(UIColor *)color attributes:(NSDictionary<NSString *, id> *)attrs{
    
    NSMutableDictionary *metrics = [self getDefaultAttributes];
    [metrics addEntriesFromDictionary:attrs];
 
    [metrics setObject:[NSNumber numberWithFloat:width] forKey:@"borderWidth"];
 
    UIView *border = [[UIView alloc ] initWithFrame:CGRectMake(0, self.bounds.size.height, self.bounds.size.width, self.bounds.size.height)];
    border.translatesAutoresizingMaskIntoConstraints = false;
    border.backgroundColor = color;
    border.tag = CustomBorderTagTop;
    [self addSubview:border];
    
    
    NSDictionary *views = @{@"border":border};
    
    // Top Border
    [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-marginLeft-[border]-marginRight-|" options:0 metrics:metrics views:views]];
    [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[border(==borderWidth)]" options:0 metrics:metrics views:views]];
 
    [self layoutIfNeeded];
 
}
 
/**
 *  Draw left border
 *
 *  @param width : CGFloat
 *  @param color : UIColor
 *  @param attrs : NSDictionary<NSString *, id>
 */
- (void)drawLeftBorder:(CGFloat)width color:(UIColor *)color attributes:(NSDictionary<NSString *, id> *)attrs{
    
    NSMutableDictionary *metrics = [self getDefaultAttributes];
    [metrics addEntriesFromDictionary:attrs];
    
    [metrics setObject:[NSNumber numberWithFloat:width] forKey:@"borderWidth"];
    
    UIView *border = [[UIView alloc ] initWithFrame:CGRectMake(0, self.bounds.size.height, self.bounds.size.width, self.bounds.size.height)];
    border.translatesAutoresizingMaskIntoConstraints = false;
    border.backgroundColor = color;
    border.tag = CustomBorderTagLeft;
    [self addSubview:border];
    
    
    NSDictionary *views = @{@"border":border};
    
    
    // Left Border
    [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"|[border(==borderWidth)]" options:0 metrics:metrics views:views]];
    [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-marginTop-[border]-marginBottom-|" options:0 metrics:metrics views:views]];
    
    
    [self layoutIfNeeded];
}
 
/**
 *  Draw right border
 *
 *  @param width : CGFloat
 *  @param color : UIColor
 *  @param attrs : NSDictionary<NSString *, id>
 */
- (void)drawRightBorder:(CGFloat)width color:(UIColor *)color attributes:(NSDictionary<NSString *, id> *)attrs{
    
    NSMutableDictionary *metrics = [self getDefaultAttributes];
    [metrics addEntriesFromDictionary:attrs];
    
    [metrics setObject:[NSNumber numberWithFloat:width] forKey:@"borderWidth"];
    
    UIView *border = [[UIView alloc ] initWithFrame:CGRectMake(0, self.bounds.size.height, self.bounds.size.width, self.bounds.size.height)];
    border.translatesAutoresizingMaskIntoConstraints = false;
    border.backgroundColor = color;
    border.tag = CustomBorderTagRight;
    [self addSubview:border];
    
    
    NSDictionary *views = @{@"border":border};
    
    // Right Border
    [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-(>=0)-[border(==borderWidth)]-marginRight-|" options:0 metrics:metrics views:views]];
    
    [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-marginTop-[border]-marginBottom-|" options:0 metrics:metrics views:views]];
    
    [self layoutIfNeeded];
}
 
/**
 *  Draw bottom border
 *
 *  @param width : CGFloat
 *  @param color : UIColor
 *  @param attrs : NSDictionary<NSString *, id>
 */
- (void)drawBottomBorder:(CGFloat)width color:(UIColor *)color attributes:(NSDictionary<NSString *, id> *)attrs{
    
    NSMutableDictionary *metrics = [self getDefaultAttributes];
    [metrics addEntriesFromDictionary:attrs];
    
    [metrics setObject:[NSNumber numberWithFloat:width] forKey:@"borderWidth"];
    
    UIView *border = [[UIView alloc ] init];
    border.translatesAutoresizingMaskIntoConstraints = false;
    border.backgroundColor = color;
    border.tag = CustomBorderTagBottom;
    [self addSubview:border];
    
    
    NSDictionary *views = @{@"border":border};
    
    // Bottom Border
    [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-marginLeft-[border]-marginRight-|" options:0 metrics:metrics views:views]];
    
    [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-(>=0)-[border(==borderWidth)]-marginBottom-|" options:0 metrics:metrics views:views]];
    
    [self layoutIfNeeded];
}
 
 
/**
 Remove Border
 
 @param type : CustomBorderType
 */
- (void)removeBorder:(CustomBorderType)type{
 
    if (type == CustomBorderAll ) {
        
        [self removeBorder:CustomBorderTop];
        [self removeBorder:CustomBorderTagLeft];
        [self removeBorder:CustomBorderTagRight];
        [self removeBorder:CustomBorderTagBottom];
        
    }else if (type == CustomBorderTop){
        
        UIView *topBorder = [self viewWithTag:CustomBorderTagTop];
        [topBorder removeFromSuperview];
 
    }else if (type == CustomBorderLeft){
        
        UIView *leftBorder = [self viewWithTag:CustomBorderTagLeft];
        [leftBorder removeFromSuperview];
 
    }else if (type == CustomBorderRight){
        
        UIView *rightBorder = [self viewWithTag:CustomBorderTagRight];
        [rightBorder removeFromSuperview];
 
    }else if (type == CustomBorderBottom){
        
        UIView *bottomBorder = [self viewWithTag:CustomBorderTagBottom];
        [bottomBorder removeFromSuperview];
 
    }
    
}
 
@end




No comments:

Post a Comment