Skip to content

Instantly share code, notes, and snippets.

@RajmohanKathiresan
Created October 5, 2016 03:20
Show Gist options
  • Save RajmohanKathiresan/3df5f2b2eb01f9b01e6fdb169ee6fadc to your computer and use it in GitHub Desktop.
Save RajmohanKathiresan/3df5f2b2eb01f9b01e6fdb169ee6fadc to your computer and use it in GitHub Desktop.
Swift 3.0 Extensions and Utility
/**
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you 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.
*/
import UIKit
/**
Extension to make rounded rect view with provided corner radius
Storyboard complaint for real time preview
*/
@IBDesignable
class RoundedRectView : UIView {
override func awakeFromNib() {
super.awakeFromNib()
}
@IBInspectable var cornerRadius:CGFloat = 0.0 {
didSet {
self.layer.cornerRadius = cornerRadius
}
}
}
/**
Extension to make rounded view
Usage: User profile picture view
*/
class RoundedView : UIView {
override func awakeFromNib() {
super.awakeFromNib()
self.layer.cornerRadius = self.bounds.size.width/2.0
}
}
/**
UIColor extensions
*/
extension UIColor {
/// Convenience initializer to create color with default alpha 1.0
public convenience init(red: Int, green: Int, blue: Int) {
self.init(red: CGFloat(red) / 255.0, green: CGFloat(green) / 255.0, blue: CGFloat(blue) / 255.0, alpha: 1.0)
}
/// Convenience initializer to create color from hex value
public convenience init(netHex:Int) {
self.init(red:(netHex >> 16) & 0xff, green:(netHex >> 8) & 0xff, blue:netHex & 0xff)
}
}
/**
FileManager extensions
*/
extension FileManager {
/// Get documents directory path for the application to store/retrieve files
static func getDocumentsDirectoryPath() -> URL {
let filemanager = FileManager.default
return filemanager.urls(for: .documentDirectory, in: .userDomainMask).first!
}
}
/**
UIViewController extension
*/
extension UIViewController {
/// Get viewcontroller instance specifying the storyboard and controller identifier
static func GetViewController(instoryboard storyboard:String, withController identifier:String) -> UIViewController {
return UIStoryboard(name: storyboard, bundle: nil).instantiateViewController(withIdentifier: identifier)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment