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
extension NSMutableAttributedString { | |
@discardableResult func bold(_ text: String) -> NSMutableAttributedString { | |
let attrs: [NSAttributedStringKey: Any] = [.font: UIFont(name: "AvenirNext-Medium", size: 12)!] | |
let boldString = NSMutableAttributedString(string:text, attributes: attrs) | |
append(boldString) | |
return self | |
} | |
@discardableResult func normal(_ text: String) -> NSMutableAttributedString { |
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
Post notification: | |
* swift: | |
NotificationCenter.default.post(name: .onCountryChanged, object: nil) | |
* objc: | |
[[NSNotificationCenter defaultCenter] postNotificationName:@"on-country-changed" object:nil]; | |
Register notification: | |
* swift: | |
NotificationCenter.default.addObserver(self, selector: #selector(self.someSelector), name: .onCountryChanged, object: nil) | |
* objc: |
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
extension UILabel { | |
func highlight(searchText: String, color: UIColor = .yellow) { | |
guard let labelText = self.text else { return } | |
do { | |
let mutableString = NSMutableAttributedString(string: labelText) | |
let regex = try NSRegularExpression(pattern: searchText, options: .caseInsensitive) | |
for match in regex.matches(in: labelText, options: NSRegularExpression.MatchingOptions(rawValue: 0), range: NSRange(location: 0, length: labelText.utf16.count)) as [NSTextCheckingResult] { | |
mutableString.addAttribute(NSBackgroundColorAttributeName, value: color, range: match.range) |
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
extension NSDate { | |
convenience init(fromString: String) { | |
let formatter = NSDateFormatter() | |
formatter.dateFormat = "yyyy/MM/dd HH:mm" | |
formatter.timeZone = NSTimeZone(name: "GMT") | |
let date = formatter.dateFromString(fromString) ?? NSDate() | |
self.init(timeInterval:0, sinceDate:date) | |
} | |
} |
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
func executionTimeInterval(block: () -> ()) -> CFTimeInterval { | |
let start = Date() // <<<<<<<<<< Start time | |
block() | |
let end = Date() // <<<<<<<<<< end time | |
let timeInterval: Double = end.timeIntervalSince(start); | |
print(timeInterval) | |
return timeInterval | |
} | |
//Example of use |
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
//Swfit 2 | |
func delay(seconds seconds: Double, completionBlock: () -> ()) { | |
let delay = seconds * Double(NSEC_PER_SEC) | |
let time = dispatch_time(DISPATCH_TIME_NOW, Int64(delay)) | |
dispatch_after(time, dispatch_get_main_queue()) { | |
completionBlock() | |
} | |
} | |
//Swift 3 |
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
import UIKit | |
import XCPlayground | |
//For more info, visit: http://blog.human-friendly.com/swift-2-xcode-7-gm-at-least-generic-support-for-at-objc-protocols | |
func reloadTableView(tableView: UITableView, dataSource: GenericTVCDatasource<String>, data: [String]) -> UITableView { | |
dataSource.reload(data) | |
tableView.reloadData() | |
return tableView | |
} |
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
import UIKit | |
import SystemConfiguration.CaptiveNetwork | |
class NetworkInfo { | |
func printSsidInfo { | |
if let ssidInfo = fetchSSIDInfo() as? [String:AnyObject], ssID = ssidInfo["SSID"] as? String { | |
println("SSID: \(ssID)") | |
} else { | |
println("SSID not found") |
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
/** Returns first non-empty SSID network info dictionary. | |
* @see CNCopyCurrentNetworkInfo | |
* remember to import this: | |
* @import SystemConfiguration.CaptiveNetwork; | |
**/ | |
+ (id)fetchSSIDInfo { | |
NSArray *ifs = (__bridge_transfer NSArray *)CNCopySupportedInterfaces(); | |
//NSLog(@"Supported interfaces: %@", ifs); | |
NSDictionary *info; |
NewerOlder