Skip to content

Instantly share code, notes, and snippets.

# 1)
# ...Parse a configuration file...
schemes = parse(file: "config.json")
# 2)
for scheme in schemes do
# 3)
# Provide environment variables for sigh using the current `scheme` object
# ...
# ENV["SIGH_USERNAME"] = scheme["apple_id"]
class KeyboardAwareViewController: UIViewController, KeyboardAvoidable {
override func viewDidLoad() {
super.viewDidLoad()
setupKeyboardNotifications()
}
deinit {
removeKeyboardNotifications()
}
}
//MARK: - Private
fileprivate extension KeyboardAvoidable where Self: UIViewController {
func keyboardWillAppear(_ notification: Notification) {
//1
let keyboardInfo = KeyboardNotification(notification)
let duration = keyboardInfo.animationDuration
let curve = keyboardInfo.animationCurve
let keyboardEndFrame = keyboardInfo.keyboardFrameEnd
//MARK: - Public
extension KeyboardAvoidable where Self: UIViewController {
func setupKeyboardNotifications() {
NotificationCenter.default.addObserver(forName: .UIKeyboardWillShow, object: nil, queue: nil, using: {
[weak self] notification in
//Had to use block because protocols don't understand selectors
//Therefore whoever conforms to `KeyboardAvoidable` needs to call `removeKeyboardNotifications` in deinit
self?.keyboardWillAppear(notification)
})
protocol KeyboardAvoidable: class {
var keyboardOpen: Bool { get set }
}
extension NSAttributedString {
func align(_ alignment: NSTextAlignment) -> NSAttributedString {
//1
guard !string.isEmpty else { return self }
//2
let mutable = mutableCopy() as! NSMutableAttributedString
//3
"Style me"
.toAttributedString()
.medium(sized: 16)
.colored(.orange)
extension NSAttributedString {
func medium(sized size: CGFloat) -> NSAttributedString {
//1
guard !string.isEmpty else { return }
//2
let mutable = mutableCopy() as! NSMutableAttributedString
//3
extension String {
func toAttributedString() -> NSAttributedString {
return NSAttributedString(string: self)
}
}
//Example Usage
"Convert me please".toAttributedString()
let attributes: [String : Any] = [
NSFontAttributeName : UIFont(name: "AvenirNext-Medium", size: 16),
NSForegroundColorAttributeName : UIColor.orange
]
let label = UILabel()
label.attributedText = NSAttributedString(string: "Style me", attributes: attributes)