Skip to content

Instantly share code, notes, and snippets.

View mlabraca's full-sized avatar

Miguel Angel Labraca mlabraca

  • The Knot WorldWide
  • Spain
View GitHub Profile
@mlabraca
mlabraca / UINavigationBar+Flat.swift
Created November 30, 2018 09:45
An extension that adds a "flat" field to UINavigationBar. This flag, when enabled, removes the shadow under the navigation bar.
import UIKit
private var flatAssociatedObjectKey: UInt8 = 0
/*
An extension that adds a "flat" field to UINavigationBar. This flag, when
enabled, removes the shadow under the navigation bar.
*/
extension UINavigationBar {
var flat: Bool {
@mlabraca
mlabraca / EasyAttributeString.swift
Created November 24, 2017 15:31
Extension to apply bold attribute to a text. Seen on [stackoverflow|https://stackoverflow.com/a/37992022/1211664]
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 {
@mlabraca
mlabraca / NotificationsCheatSheet.swift
Last active March 2, 2017 17:32
Cheat sheet for notification center (objc & swift)
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:
@mlabraca
mlabraca / UILabel+Highlight.swift
Last active January 12, 2017 15:24
UILabel extension to highlight specified text
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)
@mlabraca
mlabraca / SortingDates.swift
Last active June 23, 2016 10:00
Shows how to sort array of dates ascending and descending. As a extra, is supplied an NSDate extension to init NSDate from string with format yyyy/MM/dd HH:mm.
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)
}
}
@mlabraca
mlabraca / ExecutionTime.swift
Last active June 29, 2018 07:26
For a given block of code, calculates the execution time.
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
@mlabraca
mlabraca / ExecuteAfter.swift
Last active November 2, 2016 14:22
Executes the block after a delay of x seconds.
//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
@mlabraca
mlabraca / TableView.swift
Created December 4, 2015 17:12
TableView swift playground
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
}
@mlabraca
mlabraca / NetworkInfo.swift
Created August 13, 2015 11:15
Get Wifi network information in swift
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")
@mlabraca
mlabraca / NetworkInfo.m
Created August 13, 2015 11:12
Get Wifi network information in Objective-C
/** 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;