Skip to content

Instantly share code, notes, and snippets.

@shishirthedev
Last active January 20, 2019 05:05
Show Gist options
  • Select an option

  • Save shishirthedev/742b277fc603a86f4117e2125ce32f08 to your computer and use it in GitHub Desktop.

Select an option

Save shishirthedev/742b277fc603a86f4117e2125ce32f08 to your computer and use it in GitHub Desktop.
// String Extensions to encode/decode to Base64, capitalized first character.....
extension String{
// Decode String from Base64
func fromBase64() -> String? {
guard let data = Data(base64Encoded: self) else {
return nil
}
return String(data: data, encoding: .utf8)
}
// Encode String to Base64
func toBase64() -> String {
return Data(self.utf8).base64EncodedString()
}
// Make First Character Capitalized
func capitalizingFirstLetter() -> String {
let first = String(self.prefix(1)).capitalized
let other = String(self.dropFirst())
return first + other
}
}
// View Controller Extensions to hide keyboard on touch.................
extension UIViewController {
func hideKeyboardWhenTappedAround() {
let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(UIViewController.dismissKeyboard))
tap.cancelsTouchesInView = false
view.addGestureRecognizer(tap)
}
@objc func dismissKeyboard() {
view.endEditing(true)
}
}
// TextField Extensions to shake textfield
extension UITextField {
func isError(baseColor: CGColor, numberOfShakes shakes: Float, revert: Bool) {
let animation: CABasicAnimation = CABasicAnimation(keyPath: "shadowColor")
animation.fromValue = baseColor
animation.toValue = UIColor.red.cgColor
animation.duration = 0.4
if revert { animation.autoreverses = true } else { animation.autoreverses = false }
self.layer.add(animation, forKey: "")
let shake: CABasicAnimation = CABasicAnimation(keyPath: "position")
shake.duration = 0.07
shake.repeatCount = shakes
if revert { shake.autoreverses = true } else { shake.autoreverses = false }
shake.fromValue = NSValue(cgPoint: CGPoint(x: self.center.x - 10, y: self.center.y))
shake.toValue = NSValue(cgPoint: CGPoint(x: self.center.x + 10, y: self.center.y))
self.layer.add(shake, forKey: "position")
}
}
// UILabel Extension to shake Label
extension UILabel {
func isError(baseColor: CGColor, numberOfShakes shakes: Float, revert: Bool) {
let animation: CABasicAnimation = CABasicAnimation(keyPath: "shadowColor")
animation.fromValue = baseColor
animation.toValue = UIColor.red.cgColor
animation.duration = 0.4
if revert { animation.autoreverses = true } else { animation.autoreverses = false }
self.layer.add(animation, forKey: "")
let shake: CABasicAnimation = CABasicAnimation(keyPath: "position")
shake.duration = 0.07
shake.repeatCount = shakes
if revert { shake.autoreverses = true } else { shake.autoreverses = false }
shake.fromValue = NSValue(cgPoint: CGPoint(x: self.center.x - 10, y: self.center.y))
shake.toValue = NSValue(cgPoint: CGPoint(x: self.center.x + 10, y: self.center.y))
self.layer.add(shake, forKey: "position")
}
}
// Method to called textfiled shake extension....
func shake(_ textField:UITextField){
textField.isError(baseColor: UIColor.gray.cgColor, numberOfShakes: 2, revert: true)
}
// Method to call label shake extension....
func shakeLabel(_ label: UILabel){
label.isError(baseColor: UIColor.gray.cgColor, numberOfShakes: 2, revert: true)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment