From cff8f12bea05a77dd38b387d0c77f9ca32db28c0 Mon Sep 17 00:00:00 2001 From: Fabrizio Brancati Date: Sun, 6 Nov 2016 20:19:49 +0100 Subject: [PATCH] Refactoring & Testing --- Sources/BFKit/iOS/UIKit/UIViewExtension.swift | 46 +++++++---------- .../iOS/UIKit/UIViewExtensionTests.swift | 49 +++++++++++++++++++ 2 files changed, 68 insertions(+), 27 deletions(-) diff --git a/Sources/BFKit/iOS/UIKit/UIViewExtension.swift b/Sources/BFKit/iOS/UIKit/UIViewExtension.swift index 59c6e424..2137fe39 100644 --- a/Sources/BFKit/iOS/UIKit/UIViewExtension.swift +++ b/Sources/BFKit/iOS/UIKit/UIViewExtension.swift @@ -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 @@ -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. @@ -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. diff --git a/Tests/BFKitTests/iOS/UIKit/UIViewExtensionTests.swift b/Tests/BFKitTests/iOS/UIKit/UIViewExtensionTests.swift index f54012e9..ba3ab16d 100644 --- a/Tests/BFKitTests/iOS/UIKit/UIViewExtensionTests.swift +++ b/Tests/BFKitTests/iOS/UIKit/UIViewExtensionTests.swift @@ -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) + } }