Last active
August 29, 2015 14:23
-
-
Save ded/c547bcef1cbec8ddf019 to your computer and use it in GitHub Desktop.
Swiftys
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// custom RegEx Class | |
// let string: String = "foo bar baz" | |
// let regex: Regex = "foo" | |
// regex.match(string) // true | |
// "foo".match(string) // true | |
// "foo bar baz" =~ "foo" // true | |
struct Regex { | |
let pattern: String | |
let options: NSRegularExpressionOptions! | |
private var matcher: NSRegularExpression { | |
return NSRegularExpression(pattern: self.pattern, options: self.options, error: nil) | |
} | |
init(pattern: String, options: NSRegularExpressionOptions = nil) { | |
self.pattern = pattern | |
self.options = options | |
} | |
func match(string: String, options: NSMatchingOptions = nil) -> Bool { | |
return self.matcher.numberOfMatchesInString(string, options: options, range: NSMakeRange(0, string.utf16Count)) != 0 | |
} | |
} | |
extension Regex: StringLiteralConvertible { | |
typealias ExtendedGraphemeClusterLiteralType = StringLiteralType | |
init(unicodeScalarLiteral value: UnicodeScalarLiteralType) { | |
self.pattern = "\(value)" | |
} | |
init(extendedGraphemeClusterLiteral value: ExtendedGraphemeClusterLiteralType) { | |
self.pattern = value | |
} | |
init(stringLiteral value: StringLiteralType) { | |
self.pattern = value | |
} | |
} | |
// convert a hex color to a proper UIColor | |
// someUIControler.backgroundColor = hexColor("#22CD7E") | |
func hexColor (hex:String) -> UIColor { | |
var cString:String = hex.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet() as NSCharacterSet).uppercaseString | |
if (cString.hasPrefix("#")) { | |
cString = cString.substringFromIndex(advance(cString.startIndex, 1)) | |
} | |
if (count(cString) != 6) { | |
return UIColor.grayColor() | |
} | |
var rgbValue:UInt32 = 0 | |
NSScanner(string: cString).scanHexInt(&rgbValue) | |
return UIColor( | |
red: CGFloat((rgbValue & 0xFF0000) >> 16) / 255.0, | |
green: CGFloat((rgbValue & 0x00FF00) >> 8) / 255.0, | |
blue: CGFloat(rgbValue & 0x0000FF) / 255.0, | |
alpha: CGFloat(1.0) | |
) | |
} | |
tableView.separatorStyle = UITableViewCellSeparatorStyle.None | |
// customize the line below table cells | |
var cell = self.tableView.dequeueReusableCellWithIdentifier("tweet", forIndexPath: indexPath) as! UITableViewCell | |
(cell.contentView.viewWithTag(10) as! UIImageView).image = UIImage(named: "loader.gif") | |
(cell.contentView.viewWithTag(9) as! UILabel).text = "" | |
var lineView = UIView(frame: CGRectMake(0, cell.contentView.frame.size.height - 1.0, cell.contentView.frame.size.width, 1)) | |
lineView.backgroundColor = hexColor("#222222") | |
cell.contentView.addSubview(lineView) | |
// bring a button above other views | |
self.view.bringSubviewToFront(self.someUIButton) | |
// user takes a screenshot | |
let mainQueue = NSOperationQueue.mainQueue() | |
NSNotificationCenter.defaultCenter().addObserverForName(UIApplicationUserDidTakeScreenshotNotification, | |
object: nil, | |
queue: mainQueue) { notification in | |
println("user took a screenshot") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment