Skip to content

Instantly share code, notes, and snippets.

@KalpeshTalkar
Last active May 25, 2017 10:47
Show Gist options
  • Save KalpeshTalkar/66a5141138931b9358a562a0fb8420ac to your computer and use it in GitHub Desktop.
Save KalpeshTalkar/66a5141138931b9358a562a0fb8420ac to your computer and use it in GitHub Desktop.
UIUtils is a utility file written in Swift 3. It has UIView extension for clipping, applying corner radius, drop shadow. It features device details, screen size, device orientation, etc.
// Copyright © 2017 Kalpesh Talkar. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// For support: https://gist.github.com/KalpeshTalkar/66a5141138931b9358a562a0fb8420ac
//
fileprivate struct DefaultCardStyle {
static let CardCornerRadius = CGFloat(3)
static let CardBorderWidth = CGFloat(0.5)
static let CardShadowRadius = CGFloat(3)
static let CardShadowOpacity = Float(0.5)
static let CardShadowOffset = CGSize(width: 0, height: 0)
static let CardShadowColor = UIColor.gray.cgColor
static let CardBorderColor = UIColor.lightGray.withAlphaComponent(0.6).cgColor
}
struct Device {
static let Current = UIDevice.current
static let isiPad = Device.Current.userInterfaceIdiom == .pad
static let isiPhone = Device.Current.userInterfaceIdiom == .phone
}
struct Orientation {
static let orientation = Device.Current.orientation
static let isLandscapeLeft = Orientation.orientation == .landscapeLeft
static let isLandscapeRight = Orientation.orientation == .landscapeRight
static let isLandscape = Orientation.orientation == .landscapeLeft || Device.Current.orientation == .landscapeRight
static let isPortrait = Orientation.orientation == .portrait
static let isPortraitUpsideDown = Orientation.orientation == .portraitUpsideDown
static let isFaceUp = Orientation.orientation == .faceUp
static let isFaceDown = Orientation.orientation == .faceDown
}
struct Screen {
static let Main = UIScreen.main
static let ScreenSize = Screen.Main.bounds
static let ScreenWidth = Screen.ScreenSize.width
static let ScreenHeight = Screen.ScreenSize.height
static let ScreenScale = Screen.Main.scale
}
extension UIView {
func clipWithCornerRadius(radius: CGFloat) {
layer.cornerRadius = radius
clipsToBounds = true
}
func applyCardDesign() {
clipsToBounds = false
layer.masksToBounds = false
layer.borderWidth = DefaultCardStyle.CardBorderWidth
layer.borderColor = DefaultCardStyle.CardBorderColor
layer.cornerRadius = DefaultCardStyle.CardCornerRadius
layer.shadowRadius = DefaultCardStyle.CardShadowRadius
layer.shadowOffset = DefaultCardStyle.CardShadowOffset
layer.shadowOpacity = DefaultCardStyle.CardShadowOpacity
layer.shadowColor = DefaultCardStyle.CardShadowColor
}
func addBorder(width: CGFloat, color: UIColor) {
layer.borderWidth = width
layer.borderColor = color.cgColor
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment