Skip to content

Commit

Permalink
Refactoring & Testing
Browse files Browse the repository at this point in the history
  • Loading branch information
FabrizioBrancati committed Nov 6, 2016
1 parent 8982e98 commit cff8f12
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 27 deletions.
46 changes: 19 additions & 27 deletions Sources/BFKit/iOS/UIKit/UIViewExtension.swift
Expand Up @@ -82,7 +82,7 @@ public extension UIView {
/// - color: Border color.
/// - radius: Border radius.
/// - width: Border width.
public func borders(color: UIColor, radius: CGFloat, width: CGFloat) {
public func border(color: UIColor, radius: CGFloat, width: CGFloat) {
self.layer.borderWidth = width
self.layer.cornerRadius = radius
self.layer.shouldRasterize = false
Expand All @@ -95,17 +95,11 @@ public extension UIView {
}

/// Removes border around the UIView.
public func removeBorder() {
public func removeBorder(maskToBounds: Bool = true) {
self.layer.borderWidth = 0
self.layer.cornerRadius = 0
self.layer.borderColor = nil
}

/// Removes shadow around the UIView.
public func removeShadow() {
self.layer.shadowColor = UIColor.clear.cgColor
self.layer.shadowOpacity = 0.0
self.layer.shadowOffset = CGSize(width: 0.0, height: 0.0)
self.layer.masksToBounds = maskToBounds
}

/// Set the corner radius of UIView only at the given corner.
Expand All @@ -127,30 +121,28 @@ public extension UIView {
/// - offset: Shadow offset.
/// - opacity: Shadow opacity.
/// - radius: Shadow radius.
public func rectShadow(offset: CGSize, opacity: Float, radius: CGFloat) {
self.layer.shadowColor = UIColor.black.cgColor
/// - color: Shadow color. Default is black.
public func shadow(offset: CGSize, opacity: Float, radius: CGFloat, cornerRadius: CGFloat = 0, color: UIColor = UIColor.black) {
self.layer.shadowColor = color.cgColor
self.layer.shadowOpacity = opacity
self.layer.shadowOffset = offset
self.layer.shadowRadius = radius
if cornerRadius != 0 {
self.layer.cornerRadius = cornerRadius
self.layer.shadowPath = UIBezierPath(roundedRect: self.bounds, cornerRadius: cornerRadius).cgPath
}
self.layer.masksToBounds = false
}

/// Create a corner radius shadow on the UIView
///
/// - Parameters:
/// - cornerRadius: Corner radius value
/// - offset: Shadow offset
/// - opacity: Shadow opacity
/// - radius: Shadow radius
public func cornerRadiusShadow(cornerRadius: CGFloat, offset: CGSize, opacity: Float, radius: CGFloat) {
self.layer.shadowColor = UIColor.black.cgColor
self.layer.shadowOpacity = opacity
self.layer.shadowOffset = offset
self.layer.shadowRadius = radius
self.layer.shouldRasterize = true
self.layer.cornerRadius = cornerRadius
self.layer.shadowPath = UIBezierPath(roundedRect: self.bounds, cornerRadius: cornerRadius).cgPath
self.layer.masksToBounds = false
/// Removes shadow around the UIView.
public func removeShadow(maskToBounds: Bool = true) {
self.layer.shadowColor = nil
self.layer.shadowOpacity = 0.0
self.layer.shadowOffset = CGSize(width: 0, height: 0)
self.layer.shadowRadius = 0
self.layer.cornerRadius = 0
self.layer.shadowPath = nil
self.layer.masksToBounds = maskToBounds
}

/// Create a linear gradient.
Expand Down
49 changes: 49 additions & 0 deletions Tests/BFKitTests/iOS/UIKit/UIViewExtensionTests.swift
Expand Up @@ -17,4 +17,53 @@ class UIViewExtensionTests: XCTestCase {
override func tearDown() {
super.tearDown()
}

func testBorderColorRadiusWidth() {
let view = UIView(frame: CGRect(x: 0, y: 0, width: 200, height: 100))
view.border(color: UIColor.red, radius: 5, width: 2)

XCTAssert(view.layer.borderWidth == 2)
XCTAssert(view.layer.cornerRadius == 5)
XCTAssert(view.layer.borderColor == UIColor.red.cgColor)
}

func testRemoveBorder() {
let view = UIView(frame: CGRect(x: 0, y: 0, width: 200, height: 100))
view.border(color: UIColor.red, radius: 5, width: 2)
view.removeBorder()

XCTAssert(view.layer.borderWidth == 0)
XCTAssert(view.layer.cornerRadius == 0)
XCTAssert(view.layer.borderColor == nil)
}

func testCornerRadiusCornersRadius() {
let view = UIView(frame: CGRect(x: 0, y: 0, width: 200, height: 100))
view.cornerRadius(corners: .allCorners, radius: 5)

XCTAssertNotNil(view.layer.mask)
}

func testShadowOffsetOpacityRadiusCornerRadiusColor() {
let view = UIView(frame: CGRect(x: 0, y: 0, width: 200, height: 100))
view.shadow(offset: CGSize(width: 5, height: 5), opacity: 0.5, radius: 5, cornerRadius: 5, color: UIColor.red)

XCTAssert(view.layer.shadowColor == UIColor.red.cgColor)
XCTAssert(view.layer.shadowOpacity == 0.5)
XCTAssert(view.layer.shadowOffset == CGSize(width: 5, height: 5))
XCTAssert(view.layer.shadowRadius == 5)
XCTAssert(view.layer.cornerRadius == 5)
}

func testRemoveShadow() {
let view = UIView(frame: CGRect(x: 0, y: 0, width: 200, height: 100))
view.shadow(offset: CGSize(width: 5, height: 5), opacity: 0.5, radius: 5, cornerRadius: 5, color: UIColor.red)
view.removeShadow()

XCTAssert(view.layer.shadowColor == nil)
XCTAssert(view.layer.shadowOpacity == 0)
XCTAssert(view.layer.shadowOffset == CGSize(width: 0, height: 0))
XCTAssert(view.layer.shadowRadius == 0)
XCTAssert(view.layer.cornerRadius == 0)
}
}

0 comments on commit cff8f12

Please sign in to comment.