Created
November 12, 2015 04:41
-
-
Save rajohns08/04ccee3d74b909c139a1 to your computer and use it in GitHub Desktop.
iOS / Swift - UITextField subclass for handling dismissing when return key tapped, padding for placeholder text, turning off auto correction, and enabling clear button
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
import UIKit | |
@IBDesignable | |
class BaseTextField: UITextField { | |
let leftPadding: CGFloat = 20 | |
override func awakeFromNib() { | |
self.delegate = self | |
self.returnKeyType = UIReturnKeyType.Done | |
self.autocorrectionType = UITextAutocorrectionType.No | |
self.clearButtonMode = UITextFieldViewMode.WhileEditing | |
} | |
override func textRectForBounds(bounds: CGRect) -> CGRect { | |
return CGRectInset(bounds, leftPadding, 0) | |
} | |
override func editingRectForBounds(bounds: CGRect) -> CGRect { | |
return CGRectInset(bounds, leftPadding, 0) | |
} | |
} | |
extension BaseTextField: UITextFieldDelegate { | |
func textFieldShouldReturn(textField: UITextField) -> Bool { | |
textField.resignFirstResponder() | |
return false | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment