diff --git a/CEMKit-Swift.podspec b/CEMKit-Swift.podspec index 828b22b..1a66240 100644 --- a/CEMKit-Swift.podspec +++ b/CEMKit-Swift.podspec @@ -16,7 +16,7 @@ Pod::Spec.new do |s| # s.name = "CEMKit-Swift" - s.version = "0.1.4" + s.version = "0.1.5" s.summary = "UIKit toolset for quick prototyping and rapid development" s.description = <<-DESC @@ -64,7 +64,7 @@ Pod::Spec.new do |s| s.platform = :ios, "8.0" # When using multiple platforms - # s.ios.deployment_target = "5.0" + s.ios.deployment_target = "8.0" # s.osx.deployment_target = "10.7" @@ -74,7 +74,7 @@ Pod::Spec.new do |s| # Supports git, hg, bzr, svn and HTTP. # - s.source = { :git => "https://github.com/cemolcay/CEMKit-Swift.git", :tag => "v0.1.4" } + s.source = { :git => "https://github.com/cemolcay/CEMKit-Swift.git", :tag => "v0.1.5" } # ――― Source Code ―――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――― # diff --git a/CEMKit-Swift.xcodeproj/project.xcworkspace/xcuserdata/cem.xcuserdatad/UserInterfaceState.xcuserstate b/CEMKit-Swift.xcodeproj/project.xcworkspace/xcuserdata/cem.xcuserdatad/UserInterfaceState.xcuserstate index d8c4310..4765ba5 100644 Binary files a/CEMKit-Swift.xcodeproj/project.xcworkspace/xcuserdata/cem.xcuserdatad/UserInterfaceState.xcuserstate and b/CEMKit-Swift.xcodeproj/project.xcworkspace/xcuserdata/cem.xcuserdatad/UserInterfaceState.xcuserstate differ diff --git a/CEMKit-Swift.xcodeproj/project.xcworkspace/xcuserdata/cem.xcuserdatad/WorkspaceSettings.xcsettings b/CEMKit-Swift.xcodeproj/project.xcworkspace/xcuserdata/cem.xcuserdatad/WorkspaceSettings.xcsettings new file mode 100644 index 0000000..659c876 --- /dev/null +++ b/CEMKit-Swift.xcodeproj/project.xcworkspace/xcuserdata/cem.xcuserdatad/WorkspaceSettings.xcsettings @@ -0,0 +1,10 @@ + + + + + HasAskedToTakeAutomaticSnapshotBeforeSignificantChanges + + SnapshotAutomaticallyBeforeSignificantChanges + + + diff --git a/CEMKit-Swift/CEMKit.swift b/CEMKit-Swift/CEMKit.swift index 7d92b23..4e1fe24 100644 --- a/CEMKit-Swift/CEMKit.swift +++ b/CEMKit-Swift/CEMKit.swift @@ -15,7 +15,6 @@ import UIKit // let APPDELEGATE: AppDelegate = UIApplication.sharedApplication().delegate as AppDelegate - // MARK: - UIView let UIViewAnimationDuration: NSTimeInterval = 1 @@ -27,13 +26,14 @@ extension UIView { // MARK: Custom Initilizer - convenience init (x: CGFloat, + convenience init ( + x: CGFloat, y: CGFloat, w: CGFloat, h: CGFloat) { self.init (frame: CGRect (x: x, y: y, width: w, height: h)) } - + convenience init (superView: UIView) { self.init (frame: CGRect (origin: CGPointZero, size: superView.size)) } @@ -335,10 +335,9 @@ extension UIView { func pop () { setScale(1.1, y: 1.1) - spring(0.2) { - [unowned self] in + spring(0.2, animations: { [unowned self] () -> Void in self.setScale(1, y: 1) - } + }) } } @@ -449,6 +448,44 @@ extension UIView { +// MARK: - UIScrollView + +extension UIScrollView { + + var contentHeight: CGFloat { + get { + return contentSize.height + } set (value) { + contentSize = CGSize (width: contentSize.width, height: value) + } + } + + var contentWidth: CGFloat { + get { + return contentSize.height + } set (value) { + contentSize = CGSize (width: value, height: contentSize.height) + } + } + + var offsetX: CGFloat { + get { + return contentOffset.x + } set (value) { + contentOffset = CGPoint (x: value, y: contentOffset.y) + } + } + + var offsetY: CGFloat { + get { + return contentOffset.y + } set (value) { + contentOffset = CGPoint (x: contentOffset.x, y: value) + } + } +} + + // MARK: - UIViewController extension UIViewController { @@ -557,7 +594,7 @@ extension UIViewController { func pop () { navigationController?.popViewControllerAnimated(true) } - + func present (vc: UIViewController) { presentViewController(vc, animated: true, completion: nil) } @@ -568,46 +605,6 @@ extension UIViewController { } - -// MARK: - UIScrollView - -extension UIScrollView { - - var contentHeight: CGFloat { - get { - return contentSize.height - } set (value) { - contentSize = CGSize (width: contentSize.width, height: value) - } - } - - var contentWidth: CGFloat { - get { - return contentSize.height - } set (value) { - contentSize = CGSize (width: value, height: contentSize.height) - } - } - - var offsetX: CGFloat { - get { - return contentOffset.x - } set (value) { - contentOffset = CGPoint (x: value, y: contentOffset.y) - } - } - - var offsetY: CGFloat { - get { - return contentOffset.y - } set (value) { - contentOffset = CGPoint (x: contentOffset.x, y: value) - } - } -} - - - // MARK: - UILabel private var UILabelAttributedStringArray: UInt8 = 0 @@ -648,7 +645,8 @@ extension UILabel { - func updateAttributedStringAtIndex (index: Int, + func updateAttributedStringAtIndex ( + index: Int, attributedString: NSAttributedString) { if let att = attributedStrings?[index] { @@ -664,16 +662,17 @@ extension UILabel { } } - func updateAttributedStringAtIndex (index: Int, + func updateAttributedStringAtIndex ( + index: Int, newText: String) { if let att = attributedStrings?[index] { let newAtt = NSMutableAttributedString (string: newText) - att.enumerateAttributesInRange(NSMakeRange(0, countElements(att.string)-1), + att.enumerateAttributesInRange(NSMakeRange(0, count(att.string)-1), options: NSAttributedStringEnumerationOptions.LongestEffectiveRangeNotRequired, usingBlock: { (attribute, range, stop) -> Void in for (key, value) in attribute { - newAtt.addAttribute(key as String, value: value, range: range) + newAtt.addAttribute(key as! String, value: value, range: range) } } ) @@ -907,10 +906,10 @@ extension NSAttributedString { func addAtt (attribute: [NSString: NSObject]) -> NSAttributedString { let mutable = NSMutableAttributedString (attributedString: self) - let count = countElements(string) + let c = count(string) for (key, value) in attribute { - mutable.addAttribute(key, value: value, range: NSMakeRange(0, count)) + mutable.addAttribute(key as! String, value: value, range: NSMakeRange(0, c)) } return mutable @@ -928,7 +927,7 @@ extension NSAttributedString { font: UIFont, style: NSAttributedStringStyle = .plain) { - var atts = [NSFontAttributeName: font, NSForegroundColorAttributeName: color] + var atts = [NSFontAttributeName as NSString: font, NSForegroundColorAttributeName as NSString: color] atts += style.attribute() self.init (string: text, attributes: atts) @@ -962,43 +961,43 @@ extension String { // MARK: - UIFont +enum FontType: String { + case Regular = "Regular" + case Bold = "Bold" + case DemiBold = "DemiBold" + case Light = "Light" + case UltraLight = "UltraLight" + case Italic = "Italic" + case Thin = "Thin" + case Book = "Book" + case Roman = "Roman" + case Medium = "Medium" + case MediumItalic = "MediumItalic" + case CondensedMedium = "CondensedMedium" + case CondensedExtraBold = "CondensedExtraBold" + case SemiBold = "SemiBold" + case BoldItalic = "BoldItalic" + case Heavy = "Heavy" +} + +enum FontName: String { + case HelveticaNeue = "HelveticaNeue" + case Helvetica = "Helvetica" + case Futura = "Futura" + case Menlo = "Menlo" + case Avenir = "Avenir" + case AvenirNext = "AvenirNext" + case Didot = "Didot" + case AmericanTypewriter = "AmericanTypewriter" + case Baskerville = "Baskerville" + case Geneva = "Geneva" + case GillSans = "GillSans" + case SanFranciscoDisplay = "SanFranciscoDisplay" + case Seravek = "Seravek" +} + extension UIFont { - enum FontType: String { - case Regular = "Regular" - case Bold = "Bold" - case DemiBold = "DemiBold" - case Light = "Light" - case UltraLight = "UltraLight" - case Italic = "Italic" - case Thin = "Thin" - case Book = "Book" - case Roman = "Roman" - case Medium = "Medium" - case MediumItalic = "MediumItalic" - case CondensedMedium = "CondensedMedium" - case CondensedExtraBold = "CondensedExtraBold" - case SemiBold = "SemiBold" - case BoldItalic = "BoldItalic" - case Heavy = "Heavy" - } - - enum FontName: String { - case HelveticaNeue = "HelveticaNeue" - case Helvetica = "Helvetica" - case Futura = "Futura" - case Menlo = "Menlo" - case Avenir = "Avenir" - case AvenirNext = "AvenirNext" - case Didot = "Didot" - case AmericanTypewriter = "AmericanTypewriter" - case Baskerville = "Baskerville" - case Geneva = "Geneva" - case GillSans = "GillSans" - case SanFranciscoDisplay = "SanFranciscoDisplay" - case Seravek = "Seravek" - } - class func PrintFontFamily (font: FontName) { let arr = UIFont.fontNamesForFamilyName(font.rawValue) for name in arr { @@ -1022,15 +1021,15 @@ extension UIFont { class func AvenirNext ( type: FontType, size: CGFloat) -> UIFont { - return UIFont.Font(UIFont.FontName.AvenirNext, type: type, size: size) + return UIFont.Font(.AvenirNext, type: type, size: size) } class func AvenirNextDemiBold (size: CGFloat) -> UIFont { - return AvenirNext(UIFont.FontType.DemiBold, size: size) + return AvenirNext(.DemiBold, size: size) } class func AvenirNextRegular (size: CGFloat) -> UIFont { - return AvenirNext(UIFont.FontType.Regular, size: size) + return AvenirNext(.Regular, size: size) } } @@ -1069,7 +1068,7 @@ extension UIImageView { image: UIImage) { self.init (frame: CGRect (x: x, y: y, width: image.aspectWidthForHeight(height), height: height), image: image) } - + func imageWithUrl (url: String) { imageRequest(url, { (image) -> Void in @@ -1088,8 +1087,8 @@ extension UIImageView { }) } - func imageWithUrl (url: String, placeholder: String) { - self.image = UIImage (named: placeholder) + func imageWithUrl (url: String, placeholderNamed: String) { + self.image = UIImage (named: placeholderNamed) imageRequest(url, { (image) -> Void in if let img = image { self.image = image @@ -1209,7 +1208,7 @@ extension UIColor { var hexValue: CUnsignedLongLong = 0 if scanner.scanHexLongLong(&hexValue) { - switch (countElements(hex)) { + switch (count(hex)) { case 3: red = CGFloat((hexValue & 0xF00) >> 8) / 15.0 green = CGFloat((hexValue & 0x0F0) >> 4) / 15.0 @@ -1241,6 +1240,45 @@ extension UIColor { +// MARK - UIScreen + +extension UIScreen { + + class var Orientation: UIInterfaceOrientation { + get { + return UIApplication.sharedApplication().statusBarOrientation + } + } + + class var ScreenWidth: CGFloat { + get { + if UIInterfaceOrientationIsPortrait(Orientation) { + return UIScreen.mainScreen().bounds.size.width + } else { + return UIScreen.mainScreen().bounds.size.height + } + } + } + + class var ScreenHeight: CGFloat { + get { + if UIInterfaceOrientationIsPortrait(Orientation) { + return UIScreen.mainScreen().bounds.size.height + } else { + return UIScreen.mainScreen().bounds.size.width + } + } + } + + class var StatusBarHeight: CGFloat { + get { + return UIApplication.sharedApplication().statusBarFrame.height + } + } +} + + + // MARK: - Array extension Array { @@ -1262,129 +1300,17 @@ extension Array { -// MARK: - Dictionary - -func += (inout left: Dictionary, - right: Dictionary) { - for (k, v) in right { - left.updateValue(v, forKey: k) - } -} - - - -// MARK: - Dispatch - -func delay ( - seconds: Double, - queue: dispatch_queue_t = dispatch_get_main_queue(), - after: ()->()) { - - let time = dispatch_time(DISPATCH_TIME_NOW, Int64(seconds * Double(NSEC_PER_SEC))) - dispatch_after(time, queue, after) -} - - - -// MARK: - DownloadTask - -func urlRequest ( - url: String, - success: (NSData?)->Void, - error: ((NSError)->Void)? = nil) { - NSURLConnection.sendAsynchronousRequest( - NSURLRequest (URL: NSURL (string: url)!), - queue: NSOperationQueue.mainQueue(), - completionHandler: { response, data, err in - if let e = err { - error? (e) - } else { - success (data) - } - }) -} +// MARK: - NSDate -func imageRequest ( - url: String, - success: (UIImage?)->Void) { +extension NSDate { - urlRequest(url) {data in - if let d = data { - success (UIImage (data: d)) - } - } -} - -func jsonRequest ( - url: String, - success: (AnyObject?->Void), - error: ((NSError)->Void)?) { - urlRequest( - url, - { (data)->Void in - let json: AnyObject? = dataToJsonDict(data) - success (json) - }, - { (err)->Void in - if let e = error { - e (err) - } - }) -} - -func dataToJsonDict (data: NSData?) -> AnyObject? { - - if let d = data { - var error: NSError? - let json: AnyObject? = NSJSONSerialization.JSONObjectWithData( - d, - options: NSJSONReadingOptions.AllowFragments, - error: &error) + func toString (format: String) -> String { + let formatter = NSDateFormatter () + formatter.dateFormat = format - if let e = error { - return nil - } else { - return json - } - } else { - return nil - } -} - - - -// MARK - UIScreen - -var Orientation: UIInterfaceOrientation { - get { - return UIApplication.sharedApplication().statusBarOrientation - } -} - -var ScreenWidth: CGFloat { - get { - if UIInterfaceOrientationIsPortrait(Orientation) { - return UIScreen.mainScreen().bounds.size.width - } else { - return UIScreen.mainScreen().bounds.size.height - } - } -} - -var ScreenHeight: CGFloat { - get { - if UIInterfaceOrientationIsPortrait(Orientation) { - return UIScreen.mainScreen().bounds.size.height - } else { - return UIScreen.mainScreen().bounds.size.width - } - } -} - -var StatusBarHeight: CGFloat { - get { - return UIApplication.sharedApplication().statusBarFrame.height + return formatter.stringFromDate(self) } + } @@ -1470,7 +1396,7 @@ func clamp ( value: CGFloat, minimum: CGFloat, maximum: CGFloat) -> CGFloat { - return min (maximum, max(value, minimum)) + return min (maximum, max(value, minimum)) } @@ -1481,6 +1407,7 @@ func aspectHeightForTargetAspectWidth ( return (targetAspectWidth * currentHeight) / currentWidth } + func aspectWidthForTargetAspectHeight ( currentHeight: CGFloat, currentWidth: CGFloat, @@ -1490,6 +1417,96 @@ func aspectWidthForTargetAspectHeight ( +// MARK: - Dictionary + +func += (inout left: Dictionary, + right: Dictionary) { + for (k, v) in right { + left.updateValue(v, forKey: k) + } +} + + + +// MARK: - Dispatch + +func delay ( + seconds: Double, + queue: dispatch_queue_t = dispatch_get_main_queue(), + after: ()->()) { + + let time = dispatch_time(DISPATCH_TIME_NOW, Int64(seconds * Double(NSEC_PER_SEC))) + dispatch_after(time, queue, after) +} + + + +// MARK: - DownloadTask + +func urlRequest ( + url: String, + success: (NSData?)->Void, + error: ((NSError)->Void)? = nil) { + NSURLConnection.sendAsynchronousRequest( + NSURLRequest (URL: NSURL (string: url)!), + queue: NSOperationQueue.mainQueue(), + completionHandler: { response, data, err in + if let e = err { + error? (e) + } else { + success (data) + } + }) +} + +func imageRequest ( + url: String, + success: (UIImage?)->Void) { + urlRequest(url, { (data) -> Void in + if let d = data { + success (UIImage (data: d)) + } + }) +} + +func jsonRequest ( + url: String, + success: (AnyObject?->Void), + error: ((NSError)->Void)?) { + urlRequest( + url, + { (data)->Void in + let json: AnyObject? = dataToJsonDict(data) + success (json) + }, + error: { (err)->Void in + if let e = error { + e (err) + } + }) +} + +func dataToJsonDict (data: NSData?) -> AnyObject? { + + if let d = data { + var error: NSError? + let json: AnyObject? = NSJSONSerialization.JSONObjectWithData( + d, + options: NSJSONReadingOptions.AllowFragments, + error: &error) + + if let e = error { + return nil + } else { + return json + } + } else { + return nil + } +} + + + // MARK: - UIAlertController func alert ( @@ -1557,9 +1574,6 @@ func barButtonItem ( } -// MARK: - Block Classes - - // MARK: - BlockButton @@ -1634,6 +1648,7 @@ class BlockWebView: UIWebView, UIWebViewDelegate { return true } } + } @@ -1759,138 +1774,3 @@ class BlockLongPress: UILongPressGestureRecognizer { longPressAction? (longPress) } } - - - -// MARK: BlockBadge - -class BlockBadge: UILabel { - - var attachedView: UIView! - - override var text: String? { - didSet { - sizeToFit() - h = font.pointSize+5 - w += 10 - - center = CGPoint (x: attachedView.right, y: attachedView.top) - } - } - - init (color: UIColor, font: UIFont) { - super.init(frame: CGRect(x: 0, y: 0, width: font.pointSize+5, height: font.pointSize+5)) - - self.backgroundColor = color - self.font = font - self.textAlignment = .Center - setCornerRadius(h/2) - } - - required init(coder aDecoder: NSCoder) { - super.init(coder: aDecoder) - } -} - - - -// MARK: BlockPicker - -class BlockPicker: UIPickerView, UIPickerViewDataSource, UIPickerViewDelegate { - - var items: [String]? - var didPick: ((Int)->Void)? - - init (title: String, items: [String], didPick: (index: Int) -> Void) { - super.init() - self.items = items - self.didPick = didPick - - self.delegate = self - self.dataSource = self - } - - required init(coder aDecoder: NSCoder) { - super.init(coder: aDecoder) - } - - - // MARK: UIPickerViewDataSource - - func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int { - return 1 - } - - func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int { - return items!.count - } - - - // MARK: UIPickerViewDelegate - - func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) { - didPick? (row) - } - -} - - - -// MARK: DequeuableScrollView - -class DequeuableScrollView: UIScrollView { - - private var reusableViews: [UIView] = [] - private var visibleRect: CGRect! - - - override init (frame: CGRect) { - super.init(frame: frame) - } - - required init (coder aDecoder: NSCoder) { - super.init(coder: aDecoder) - } - - - override var contentOffset: CGPoint{ - didSet { - visibleRect = CGRect(origin: contentOffset, size: frame.size) - updateReusableViews() - } - } - - override func addSubview(view: UIView) { - super.addSubview(view) - reusableViews.append(view) - updateReusableViews() - } - - func updateReusableViews () { - for v in reusableViews { - if CGRectIntersectsRect(v.frame, visibleRect) { - if let s = v.superview { - if s == self { - continue - } else { - addSubview(v) - } - } else { - addSubview(v) - } - } else { - if let s = v.superview { - if s == self { - v.removeFromSuperview() - } else { - continue - } - } else { - continue - } - } - } - } -} - - diff --git a/CEMKit-Swift/ViewController.swift b/CEMKit-Swift/ViewController.swift index ca8b1c0..d00e595 100644 --- a/CEMKit-Swift/ViewController.swift +++ b/CEMKit-Swift/ViewController.swift @@ -12,7 +12,11 @@ class ViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() - + cemkitDemo() + } + + + func cemkitDemo () { let att = NSAttributedString.withAttributedStrings({ att in att.appendAttributedString (NSAttributedString( text: "asd", @@ -30,7 +34,7 @@ class ViewController: UIViewController { text: "\nasd", color: UIColor.blackColor(), font: UIFont.Font(.AvenirNext, - type: UIFont.FontType.DemiBold, + type: .DemiBold, size: 15), style: .underline(.StyleSingle, UIColor.blueColor()))) }) @@ -56,7 +60,7 @@ class ViewController: UIViewController { let c = UILabel ( x: l.right, - y: StatusBarHeight, + y: UIScreen.StatusBarHeight, width: 150, text: "asdjndajsndajksdnkjanj\njewfwj\njfnewkfn", textColor: UIColor.blackColor(), @@ -95,7 +99,7 @@ class ViewController: UIViewController { }, { error in println("error" + error.description) - }) + }) view.addSubview(sheeter) } diff --git a/README.md b/README.md index 9bc1b4b..cdf320c 100644 --- a/README.md +++ b/README.md @@ -26,6 +26,7 @@ UIKit & Foundation toolbelt for quick prototyping and rapid development. * [UIImageView](#UIImageView) * [UIColor](#UIColor) * [UIImage](#UIImage) +* [NSDate](#NSDate) * [Array](#Array) * [Dictionary](#Dictionary) * [Dispatch](#Dispatch) @@ -939,6 +940,19 @@ Resize image based on its height (auto calculates width and keeps aspect ratio) func aspectResizeWithHeight (height: CGFloat) -> UIImage ``` +### NSDate + +##### Easy convert to string with format + +``` swift + func toString (format: String) -> String { + let formatter = NSDateFormatter () + formatter.dateFormat = format + + return formatter.stringFromDate(self) + } +``` + ### Array ##### Remove Object