IOS custom method, ios custom Method

Source: Internet
Author: User

IOS custom method, ios custom Method

Sample Code

/// // OC. h //////////////////////////

//
// UIView + FreeBorder. h
// BHBFreeBorder
//
// Created by bihongbo on 15/12/30.
// Copyright 2015 bihongbo. All rights reserved.
//

# Import <UIKit/UIKit. h>

Typedef enum: NSUInteger {
BorderTypeTop,
BorderTypeLeft,
BorderTypeRight,
BorderTypeBottom
} BorderType;

@ Interface UIView (FreeBorder)

/** Multi-border */
-(Void) addBorderWithColor :( UIColor *) color size :( CGFloat) size borderTypes :( NSArray *) types;

/** Single border */
-(Void) addBorderLayerWithColor :( UIColor *) color size :( CGFloat) size borderType :( BorderType) boderType;

@ End

// Example
/// Bottom top right border
// [Lbl1 addBorderWithColor: [UIColor redColor] size: 1 borderTypes: @ [@ (BorderTypeBottom), @ (BorderTypeTop), @ (BorderTypeRight)];
/// Single border
// [Lbl2 addBorderLayerWithColor: [UIColor greenColor] size: 1 borderType: BorderTypeRight];

/// // OC. m //////////////////////////

//
// UIView + FreeBorder. m
// BHBFreeBorder
//
// Created by bihongbo on 15/12/30.
// Copyright 2015 bihongbo. All rights reserved.
//

# Import "UIView + FreeBorder. h"

@ Implementation UIView (FreeBorder)

-(Void) addBorderWithColor :( UIColor *) color size :( CGFloat) size borderTypes :( NSArray *) types {
For (int I = 0; I <types. count; I ++ ){
[Self addBorderLayerWithColor: color size: size borderType: [types [I] integerValue];
}
}

-(Void) addBorderLayerWithColor :( UIColor *) color size :( CGFloat) size borderType :( BorderType) boderType {
CALayer * layer = [CALayer layer];
Layer. backgroundColor = color. CGColor;
[Self. layer addSublayer: layer];

Switch (boderType ){
Case BorderTypeTop:
Layer. frame = CGRectMake (0, 0, self. frame. size. width, size );
Break;
Case BorderTypeLeft:
Layer. frame = CGRectMake (0, 0, size, self. frame. size. height );
Break;
Case BorderTypeBottom:
Layer. frame = CGRectMake (0, self. frame. size. height-size, self. frame. size. width, size );
Break;
Case BorderTypeRight:
Layer. frame = CGRectMake (self. frame. size. width-size, 0, size, self. frame. size. height );
Break;
Default:
Break;
}

}

@ End

//////////// //////////////

//
// UIView + FreeBolder. swift
// Daydays
//
// Created by bihongbo on 9/14/15.
// Copyright (c) 2015 daydays. All rights reserved.
//

Import Foundation
Import UIKit

@ Objc enum BorderType: NSInteger {

Case top, left, bottom, right

}
Extension UIView {

// MARK:-adds a border to the view. The enumerated array can be filled with the top, bottom, and left sides.
@ Objc func addBorder (color: UIColor ?, Size: CGFloat, borderTypes: NSArray ){

Var currentColor: UIColor?

If let _ = color {
CurrentColor = color
} Else {
CurrentColor = UIColor. blackColor ()
}
For borderType in borderTypes {
Let bt: NSNumber = borderType! NSNumber
Self. addBorderLayer (currentColor !, Size: size, boderType: BorderType (rawValue: bt. integerValue )!)
}
}

@ Objc func addBorderLayer (color: UIColor, size: CGFloat, boderType: BorderType ){

Let layer: CALayer = CALayer ()
Layer. backgroundColor = color. CGColor
Self. layer. addSublayer (layer)

Switch boderType {

Case. top:
Layer. frame = CGRectMake (0, 0, self. frame. width, size)

Case. left:
Layer. frame = CGRectMake (0, 0, size, self. frame. height)

Case. bottom:
Layer. frame = CGRectMake (0, self. frame. height-size, self. frame. width, size)

Case. right:
Layer. frame = CGRectMake (self. frame. width-size, 0, size, self. frame. height)

// Default:
// Return;
}
}
}

// Example
/// Bottom top right border
// Lbl1.addBorder (UIColor. redColor (), size: 1, borderTypes: [BorderType. bottom. rawValue, BorderType. top. rawValue, BorderType. right. rawValue])
/// Single border
// Lbl2.addBorderLayer (UIColor. greenColor (), size: 1, boderType: BorderType. right );

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.