CEMKit-Swift 0.1.3

CEMKit-Swift 0.1.3

TestsTested
LangLanguage SwiftSwift
License MIT
ReleasedLast Release Jan 2015
SPMSupports SPM

Maintained by Cem Olcay.



  • By
  • Cem Olcay

CEMKit

UIKit toolset for quick prototyping and rapid development

Table of Contents

Documentation

UIView extension

Init

Quick init method for views
    convenience init (x: CGFloat,
        y: CGFloat,
        w: CGFloat,
        h: CGFloat)

Frame

Quick access and manupliate to CGRect values of UIView's frame
    var x: CGFloat {
        get {
            return self.frame.origin.x
        } set (value) {
            self.frame = CGRect (x: value, y: self.y, width: self.w, height: self.h)
        }
    }
    var y: CGFloat {
        get {
            return self.frame.origin.y
        } set (value) {
            self.frame = CGRect (x: self.x, y: value, width: self.w, height: self.h)
        }
    }
    var w: CGFloat {
        get {
            return self.frame.size.width
        } set (value) {
            self.frame = CGRect (x: self.x, y: self.y, width: value, height: self.h)
        }
    }
    var h: CGFloat {
        get {
            return self.frame.size.height
        } set (value) {
            self.frame = CGRect (x: self.x, y: self.y, width: self.w, height: value)
        }
    }
    var position: CGPoint {
        get {
            return self.frame.origin
        } set (value) {
            self.frame = CGRect (origin: value, size: self.frame.size)
        }
    }
    var size: CGSize {
        get {
            return self.frame.size
        } set (value) {
            self.frame = CGRect (origin: self.frame.origin, size: size)
        }
    }
Also for autolayout
    var left: CGFloat {
        get {
            return self.x
        } set (value) {
            self.x = value
        }
    }
    var right: CGFloat {
        get {
            return self.x + self.w
        } set (value) {
            self.x = value - self.w
        }
    }
    var top: CGFloat {
        get {
            return self.y
        } set (value) {
            self.y = value
        }
    }
    var bottom: CGFloat {
        get {
            return self.y + self.h
        } set (value) {
            self.y = value - self.h
        }
    }
Getting frame values with offset
    func leftWithOffset (offset: CGFloat) -> CGFloat
    func rightWithOffset (offset: CGFloat) -> CGFloat
    func topWithOffset (offset: CGFloat) -> CGFloat
    func botttomWithOffset (offset: CGFloat) -> CGFloat

Layer

Setting anchor position easily
    enum AnchorPosition: CGPoint {
        case TopLeft        = "{0, 0}"
        case TopCenter      = "{0.5, 0}"
        case TopRight       = "{1, 0}"

        case MidLeft        = "{0, 0.5}"
        case MidCenter      = "{0.5, 0.5}"
        case MidRight       = "{1, 0.5}"

        case BottomLeft     = "{0, 1}"
        case BottomCenter   = "{0.5, 1}"
        case BottomRight    = "{1, 1}"
    }
    func setAnchorPosition (anchorPosition: AnchorPosition)
Shadow, Border, Corner Radius, Stroke, Circle

Adding shadow

    func addShadow (offset: CGSize,
        radius: CGFloat,
        color: UIColor,
        opacity: Float)

Adding borders

    func addBorder (width: CGFloat,
        color: UIColor)

Setting corner radius of borders

    func setCornerRadius (radius: CGFloat)

Adding stroke to borders

    func drawStroke (width: CGFloat,
        color: UIColor)

Circle Drawing

    func drawCircle (fillColor: UIColor,
        strokeColor: UIColor,
        strokeWidth: CGFloat)

Arc Drawing

    func drawArc (from: CGFloat,
        to: CGFloat,
        clockwise: Bool,
        width: CGFloat,
        fillColor: UIColor,
        strokeColor: UIColor,
        lineCap: String)

Transform

Manuplating view's rotation and scale

Rotation

    func setRotationX (x: CGFloat)
    func setRotationY (y: CGFloat)
    func setRotationZ (z: CGFloat)

   func setRotation (x: CGFloat,
        y: CGFloat,
        z: CGFloat)

Scale

    func setScale (x: CGFloat,
        y: CGFloat)

Animation

Animating view with constant values

Constants

    let UIViewAnimationDuration: NSTimeInterval = 1
    let UIViewAnimationSpringDamping: CGFloat = 0.5
    let UIViewAnimationSpringVelocity: CGFloat = 0.5

Animation Mehtods

    func animate (animations: (()->Void)!,
        completion: ((Bool)->Void)? = nil) 

    func spring (animations: (()->Void)!,
        completion: ((Bool)->Void)? = nil)

Gestures

Adding gestures single line

Tap

    func addTapGesture (tapNumber: NSInteger,
        target: AnyObject, action: Selector)

Swipe

    func addSwipeGesture (
        direction: UISwipeGestureRecognizerDirection,  
        numberOfTouches: Int,  
        target: AnyObject,  
        action: Selector)  

Pan

    func addPanGesture (target: AnyObject,
        action: Selector)

UIViewController extension

Quick access top layout and bottom layout

Top

    var top: CGFloat {
        get {
            if let nav = self.navigationController {
                if nav.navigationBarHidden {
                    return view.top
                } else {
                    return nav.navigationBar.bottom
                }
            } else {
                return view.top
            }
        }
    }

Bottom

    var bottom: CGFloat {
        get {
            if let tab = tabBarController {
                if tab.tabBar.hidden {
                    return view.bottom
                } else {
                    return tab.tabBar.top
                }
            } else {
                return view.bottom
            }
        }
    }

Calculate app area exluding NavigationBar and TabBar

    var applicationFrame: CGRect {
        get {
            return CGRect (x: view.x, y: top, width: view.w, height: bottom - top)
        }
    }

Quick access navigation bar properties

     var navigationBar: UINavigationBar? {
        get {
            return navigationController?.navigationBar
        }
    }

Get Navigation bar height of device

    var navigationBarHeight: CGFloat {
        get {
            if let nav = self.navigationController {
                return nav.navigationBar.h
            }

            return 0
        }
    }

Get or set navigation bar color

    var navigationBarColor: UIColor? {
        get {
            return navigationController?.navigationBar.tintColor
        } set (value) {
            navigationController?.navigationBar.barTintColor = value
        }
    }

UILabel extension

NSAttributedString

AttributedStrings property for accessing, adding or updating attributedText of label
    private var UILabelAttributedStringArray: UInt8 = 0
    var attributedStrings: [NSAttributedString]? {
        get {
            return objc_getAssociatedObject(self, &UILabelAttributedStringArray) as? [NSAttributedString]
        } set (value) {
            objc_setAssociatedObject(self, &UILabelAttributedStringArray, value, UInt(OBJC_ASSOCIATION_RETAIN_NONATOMIC))
        }
    }

Adding

    func addAttributedString (text: String,
        color: UIColor,
        font: UIFont)
    func addAttributedString (attributedString: NSAttributedString)

Updating

    func updateAttributedStringAtIndex (index: Int,
        attributedString: NSAttributedString)
    func updateAttributedStringAtIndex (index: Int,
        newText: String)

Frame

Automatically sets its height by the contents
    func getEstimatedHeight () -> CGFloat
     func fitHeight ()  

String extension

Subscript for accessing characters at index of string
    subscript (i: Int) -> String {
        return String(Array(self)[i])
    }

UIFont extension

FontType and FontName enums for easily create UIFonts
    enum FontType: String
    enum FontName: String
    class func Font (name: FontName, type: FontType, size: CGFloat) -> UIFont

Even customise it for specific font

    class func HelveticaNeue (type: FontType, size: CGFloat) -> UIFont

Print all family of font on single line

    class func PrintFontFamily (font: FontName)

UIColor extension

Create random color or RGB/A colors easily in range of [0, 255]
    class func randomColor () -> UIColor
    class func RGBColor (r: CGFloat,
        g: CGFloat,
        b: CGFloat) -> UIColor
    class func RGBAColor (r: CGFloat,
        g: CGFloat,
        b: CGFloat,
        a: CGFloat) -> UIColor

UIImage extension

Resize an image with keep it aspect ratio

Calculate possible aspect width for height

    func aspectWidthForHeight (height: CGFloat) -> CGFloat

Calculate possible aspect height for width

    func aspectHeightForWidth (width: CGFloat) -> CGFloat

Resize image based on its width (auto calculates height and keeps aspect ratio)

    func aspectResizeWithWidth (width: CGFloat) -> UIImage

Resize image based on its height (auto calculates width and keeps aspect ratio)

    func aspectResizeWithHeight (height: CGFloat) -> UIImage

CGPoint

Custom operators for CGPoints
    func + (left: CGPoint, right: CGPoint) -> CGPoint
    func - (left: CGPoint, right: CGPoint) -> CGPoint
StringLiteralConvertable implementation
    public init(stringLiteral value: StringLiteralType)
    public init(extendedGraphemeClusterLiteral value: StringLiteralType)
    public init(unicodeScalarLiteral value: StringLiteralType) {
            self = CGPointFromString(value)

CGSize

Custom operators for CGSizes
    func + (left: CGSize, right: CGSize) -> CGSize
    func - (left: CGSize, right: CGSize) -> CGSize

CGFloat

Access device related mesurements

Screen Width

    var ScreenWidth: CGFloat {
        get {
            return UIScreen.mainScreen().bounds.size.width
        }
    }

Screen Height

    var ScreenHeight: CGFloat {
        get {
            return UIScreen.mainScreen().bounds.size.height
        }
    }

Status bar height

    var StatusBarHeight: CGFloat {
        get {
            return UIApplication.sharedApplication().statusBarFrame.height
        }
    }
Convert degrees to radians
    func degreesToRadians (angle: CGFloat) -> CGFloat
Normalize value to [0, 1] or vice verca

Convert [min, max] to [0, 1]

    func normalizeValue (value: CGFloat,
        min: CGFloat,
        max: CGFloat) -> CGFloat

Convert [0, 1] to to [min, max]

    func convertNormalizedValue (value: CGFloat,
        min: CGFloat,
        max: CGFloat) -> CGFloat

UIAlertViewController

Single line, block based ios 8 alert
    func alert (title: String,
        message: String,
        cancelAction: ((UIAlertAction!)->Void)? = nil,
        okAction: ((UIAlertAction!)->Void)? = nil) -> UIAlertController

UIBarButtonItem

Create bar button item with image, single line
    func barButtonItem (imageName: String,
        action: (AnyObject)->()) -> UIBarButtonItem

Create bar button item with text and color

    func barButtonItem (title: String,
        color: UIColor,
        action: (AnyObject)->()) -> UIBarButtonItem

BlockButton

Regular UIButton with actionBlock:
    var actionBlock: ((sender: BlockButton) -> ())? {
        didSet {
            self.addTarget(self, action: "action:", forControlEvents: UIControlEvents.TouchUpInside)
        }
    }

    func action (sender: BlockButton) {
        actionBlock! (sender: sender)
    }

BlockWebView

Regular UIWebView with block based actions instead of UIWebViewDelegate
    var didStartLoad: ((NSURLRequest) -> ())?
    var didFinishLoad: ((NSURLRequest) -> ())?
    var didFailLoad: ((NSURLRequest, NSError) -> ())?    
    var shouldStartLoadingRequest: ((NSURLRequest) -> (Bool))?