Created
February 20, 2017 12:11
-
-
Save litoarias/f15c628929c63fcec4591981ccf4d193 to your computer and use it in GitHub Desktop.
UITextField with left icon for Swift 3
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 HATextField: UITextField { | |
override func textRect(forBounds bounds: CGRect) -> CGRect { | |
return CGRect(x: bounds.origin.x + iconWidth*2, y: bounds.origin.y, width: bounds.width, height: bounds.height) | |
} | |
override func editingRect(forBounds bounds: CGRect) -> CGRect { | |
return CGRect(x: bounds.origin.x + iconWidth*2, y: bounds.origin.y, width: bounds.width, height: bounds.height) | |
} | |
// Provides left padding for images | |
override func leftViewRect(forBounds bounds: CGRect) -> CGRect { | |
var textRect = super.leftViewRect(forBounds: bounds) | |
textRect.origin.x += leftPadding | |
layer.cornerRadius = radius | |
return textRect | |
} | |
@IBInspectable var leftImage: UIImage? { | |
didSet { | |
updateView() | |
} | |
} | |
var iconWidth: CGFloat = 20 | |
@IBInspectable var leftPadding: CGFloat = 0 | |
@IBInspectable var radius: CGFloat = 0 | |
@IBInspectable var color: UIColor = UIColor.lightGray { | |
didSet { | |
updateView() | |
} | |
} | |
func updateView() { | |
if let image = leftImage { | |
leftViewMode = UITextFieldViewMode.always | |
let imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: iconWidth, height: iconWidth)) | |
imageView.image = image | |
// Note: In order for your image to use the tint color, you have to select the image in the Assets.xcassets and change the "Render As" property to "Template Image". | |
imageView.tintColor = color | |
leftView = imageView | |
} else { | |
leftViewMode = UITextFieldViewMode.never | |
leftView = nil | |
} | |
// Placeholder text color | |
attributedPlaceholder = NSAttributedString(string: placeholder != nil ? placeholder! : "", attributes:[NSForegroundColorAttributeName: color]) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment