Skip to content

Instantly share code, notes, and snippets.

@wujianguo
Last active November 3, 2015 09:40
Show Gist options
  • Select an option

  • Save wujianguo/a390785248f33c4789d6 to your computer and use it in GitHub Desktop.

Select an option

Save wujianguo/a390785248f33c4789d6 to your computer and use it in GitHub Desktop.
ios swift util
import UIKit
extension UIColor {
/**
* Initializes and returns a color object for the given RGB hex integer.
*/
public convenience init(rgb: Int) {
self.init(
red: CGFloat((rgb & 0xFF0000) >> 16) / 255.0,
green: CGFloat((rgb & 0x00FF00) >> 8) / 255.0,
blue: CGFloat((rgb & 0x0000FF) >> 0) / 255.0,
alpha: 1)
}
public convenience init(colorString: String) {
var colorInt: UInt32 = 0
NSScanner(string: colorString).scanHexInt(&colorInt)
self.init(rgb: (Int) (colorInt ?? 0xaaaaaa))
}
}
extension UIViewController {
var containerViewController: UIViewController? {
if let nav = self as? UINavigationController {
return nav.visibleViewController
} else {
return self
}
}
}
import CoreData
extension NSManagedObject {
class func createEntity() -> NSManagedObject {
let managedContext = (UIApplication.sharedApplication().delegate as! AppDelegate).managedObjectContext
let entityDes = NSEntityDescription.entityForName(NSStringFromClass(self).componentsSeparatedByString(".").last!, inManagedObjectContext: managedContext)!
return NSManagedObject(entity: entityDes, insertIntoManagedObjectContext: managedContext)
}
}
@IBDesignable
class CircleView: UIView {
@IBInspectable
var color: UIColor = UIColor.redColor()
override func drawRect(rect: CGRect) {
super.drawRect(rect)
let context = UIGraphicsGetCurrentContext()
let innerFrame = CGRectMake(2, 2, rect.width - 4, rect.height - 4)
CGContextSetFillColorWithColor(context, color.CGColor)
CGContextAddEllipseInRect(context, innerFrame)
CGContextFillPath(context)
}
}
@IBDesignable
class PlayButton: UIButton {
@IBInspectable
var themeColor: UIColor = UIColor.redColor() { didSet { setNeedsDisplay() } }
override func drawRect(rect: CGRect) {
super.drawRect(rect)
let context = UIGraphicsGetCurrentContext()
CGContextSetFillColorWithColor(context, themeColor.CGColor)
CGContextAddEllipseInRect(context, rect)
CGContextFillPath(context)
let radius = min(rect.height, rect.width) / 2 * 5 / 9
CGContextBeginPath(context)
CGContextMoveToPoint(context, rect.width / 2 - radius / 2, rect.height / 2 - radius * sqrt(3) / 2)
CGContextAddLineToPoint(context, rect.width / 2 - radius / 2, rect.height / 2 + radius * sqrt(3) / 2)
CGContextAddLineToPoint(context, rect.width / 2 + radius, rect.height / 2)
CGContextClosePath(context)
CGContextSetFillColorWithColor(context, UIColor.whiteColor().CGColor)
CGContextFillPath(context);
}
}
let log: XCGLogger = {
// Setup XCGLogger
let log = XCGLogger.defaultInstance()
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "HH:mm:ss.SSS"
dateFormatter.locale = NSLocale.currentLocale()
log.dateFormatter = dateFormatter
#if DEBUG
log.setup(.Debug, showThreadName: true, showLogLevel: true, showFileNames: false, showLineNumbers: true, showDate: true)
#else
log.setup(.Severe, showThreadName: true, showLogLevel: true, showFileNames: false, showLineNumbers: true, showDate: true)
#endif
return log
}()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment