Created
April 30, 2016 06:14
-
-
Save soggybag/0aeee2e090ca131576037eecf3266722 to your computer and use it in GitHub Desktop.
Adds border, corner radius, and padding to the UITextField. These properties can be set via the attribute inspector in storyboard.
This file contains hidden or 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 CustomTextField: UITextField { | |
var padding: UIEdgeInsets { | |
get { | |
return UIEdgeInsets(top: 0, left: paddingValue, bottom: 0, right: paddingValue) | |
} | |
} | |
override func textRectForBounds(bounds: CGRect) -> CGRect { | |
return UIEdgeInsetsInsetRect(bounds, padding) | |
} | |
override func placeholderRectForBounds(bounds: CGRect) -> CGRect { | |
return UIEdgeInsetsInsetRect(bounds, padding) | |
} | |
override func editingRectForBounds(bounds: CGRect) -> CGRect { | |
return UIEdgeInsetsInsetRect(bounds, padding) | |
} | |
@IBInspectable var paddingValue: CGFloat = 0 | |
@IBInspectable var borderColor: UIColor? = UIColor.clearColor() { | |
didSet { | |
layer.borderColor = self.borderColor?.CGColor | |
} | |
} | |
@IBInspectable var borderWidth: CGFloat = 0 { | |
didSet { | |
layer.borderWidth = self.borderWidth | |
} | |
} | |
@IBInspectable var cornerRadius: CGFloat = 0 { | |
didSet { | |
layer.cornerRadius = self.cornerRadius | |
layer.masksToBounds = self.cornerRadius > 0 | |
} | |
} | |
required init?(coder aDecoder: NSCoder) { | |
super.init(coder: aDecoder) | |
} | |
override init(frame: CGRect) { | |
super.init(frame: frame) | |
} | |
override func drawRect(rect: CGRect) { | |
self.layer.cornerRadius = self.cornerRadius | |
self.layer.borderWidth = self.borderWidth | |
self.layer.borderColor = self.borderColor?.CGColor | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
In Swift 4...