Convenience classes wrapped with Swift features and useful Swift extensions
AttributedString Builder encapsulated using the Swift resultBuilder feature
Before:
let build1 = NSMutableAttributedString()
let start = NSAttributedString(string: "Hello", attributes: [
NSAttributedString.Key.foregroundColor: UIColor.red
])
build1.append(start)
build1.append(NSAttributedString(string: " "))
let end = NSAttributedString(string: "World", attributes: [
NSAttributedString.Key.foregroundColor: UIColor.yellow,
NSAttributedString.Key.font: UIFont.boldSystemFont(ofSize: 18)
])
build1.append(end)
Now:
let build = NSAttributedString {
Text("Hello").color(.red)
SpecialCharacters.space
Text("World").color(.yellow).font(.boldSystemFont(ofSize: 18))
}
let alertController = ActionSheet(title: "", message: nil) {
Action.default("Share") { }
Action.default("Download") { }
Action.cancel("Cancel") { }
}
present(alertController, animated: true, completion: nil)
extension UserDefaults {
@UserDefault(key: "username", defaultValue: "Antoine van der Lee")
static var username: String
}
let subscription = UserDefaults.$username.sink { username in
print("New username: \(username)")
}
UserDefaults.username = "Test"