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
// https://hashaam.com/2017/11/13/adjust-baseline-of-text-using-nsattributedstring/ | |
let attributedString = NSAttributedString(string: “text", attributes: [ | |
NSFontAttributeName: font, | |
NSForegroundColorAttributeName: UIColor.white, | |
NSBaselineOffsetAttributeName: -5.0 | |
]) |
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
// https://hashaam.com/2017/11/13/url-encode-only-unicode-characters/ | |
let originalURLString = "https://www.exampleserver.com/articles/latest/لن-تصدق-ما-يمكن-أن-تجده-في-مطبخك-فاحذر-غسل-هذه-الأداة.html" | |
let allowedCharacterSet = CharacterSet.urlFragmentAllowed | |
let urlString = originalURLString.addingPercentEncoding(withAllowedCharacters: allowedCharacterSet)! | |
let url = URL(string: urlString)! |
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
// https://hashaam.com/2017/09/02/adjust-scroll-view-content-inset-based-on-keyboard-frame | |
func adjustForKeyboardHandler(notification: Notification) { | |
// this method assumes scrollView is defined | |
guard let userInfo = notification.userInfo else { return } | |
guard let value = userInfo[UIKeyboardFrameEndUserInfoKey] as? NSValue else { return } | |
let keyboardFrame = value.cgRectValue |
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
// https://hashaam.com/2017/09/02/adjust-scroll-view-content-inset-based-on-keyboard-frame | |
override func viewWillAppear(_ animated: Bool) { | |
NotificationCenter.default.addObserver(self, selector: #selector(adjustForKeyboardHandler(notification:)), name: NSNotification.Name.UIKeyboardWillChangeFrame, object: nil) | |
NotificationCenter.default.addObserver(self, selector: #selector(adjustForKeyboardHandler(notification:)), name: NSNotification.Name.UIKeyboardWillHide, object: nil) | |
} |
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
// https://hashaam.com/2017/09/02/get-specific-urlqueryitem-from-urlcomponents | |
if let utmTermValue = queryParam(urlString: "http://www.example.com/?utm_source=adsite&utm_campaign=adcampaign&utm_term=adkeyword", param: "utm_term") { | |
// handle utmTermValue | |
} |
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
// https://hashaam.com/2017/09/02/get-specific-urlqueryitem-from-urlcomponents | |
func queryParam(urlString: String?, param: String) -> String? { | |
guard let urlString = urlString else { return nil } | |
guard let urlComponents = URLComponents(string: urlString) else { return nil } | |
guard let queryItems = urlComponents.queryItems else { return nil } | |
return queryItems.filter { queryItem in | |
return queryItem.name == param | |
}.first?.value |
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
// https://hashaam.com/2017/09/02/get-filename-without-extension | |
let path = "hls_a128_v4.m3u8" | |
let url = URL(string: path) | |
let filename = url?.deletingPathExtension().lastPathComponent | |
print(filename) // hls_a128_v4 | |
let fileExtension = url?.pathExtension |
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
// https://hashaam.com/2017/08/31/uiwindow-presentation-context/ | |
if let delegate = UIApplication.shared.delegate, let window = delegate.window, let viewController = window?.presentationContext() { | |
// perform action on viewController | |
} |
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
// https://hashaam.com/2017/08/31/uiwindow-presentation-context/ | |
extension UIWindow { | |
func presentationContext(context: UIViewController? = nil) -> UIViewController? { | |
var presentationContextViewController = rootViewController | |
if let context = context { | |
presentationContextViewController = context | |
} |
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
// https://hashaam.com/2017/07/22/get-device-token-for-remote-push-notifications-in-swift-3/ | |
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) { | |
let tokenParts = deviceToken.map { data -> String in | |
return String(format: "%02.2hhx", data) | |
} | |
let token = tokenParts.joined() | |