Skip to content

Instantly share code, notes, and snippets.

@rajohns08
Created November 12, 2015 04:41
Show Gist options
  • Save rajohns08/04ccee3d74b909c139a1 to your computer and use it in GitHub Desktop.
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
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