This extension provides the ability to easily add action leading and trailing icons to a UITextField. Each icon can be assigned a color and an action that executes when the icon is tapped.
To learn more about this project, please visit it on GitHub.
Give UITextFieldIcons a try without having to type a single line of code with the UITextFieldIcons Demo.
Simply add UITextFieldIcons via Swift Package Manager to your project... that's it!
You can choose to create a UITextFieldIcons control in the same way that you would create a stock UITextField control, either via Storyboards, programmatically or through a mixture of the two.
- Select the UITextField that you wish to use
- Switch to the Identity Inspector
- Update the UITextField's class to 'UITextFieldIcons'
- Switch to the Attributes Inspector
- You can find the configuration options under the Text Field Icons heading
Import UITextFieldIcons. Then create a UITextFieldIcons control in the same way that you would create a UITextField control, except that instead of using UITextField, use the UITextFieldIcons type. Because UITextFieldIcons in an extension of UITextField, all of UITextField's functions will be inherited.
When a user taps on a leading or trailing icon, two ways are provided to have custom code executed.
The first execution method is via a standard closure. These can be created on leadingTapAction and trailingTapAction like so:
textField.leadingTapAction = {
print("Closure: You have tapped on the leading icon");
}
textField.trailingTapAction = {
print("Closure: You have tapped on the trailing icon");
}
The second way is via a delegate method:
- Add the
UITextFieldDelegatedelegate - Add the
UITextFieldIconsDelegatedelegate - Implment the
UITextFieldIconsLeadingIcon_Tap(sender: UITextFieldIcons)orUITextFieldIconsTrailingIcon_Tap(sender: UITextFieldIcons)methods
class ViewController: UIViewController, UITextFieldDelegate, UITextFieldIconsDelegate {
// MARK: - Delegates
internal func UITextFieldIconsLeadingIcon_Tap(sender: UITextFieldIcons) {
print("Delegate Method: You have tapped on the leading icon");
}
internal func UITextFieldIconsTrailingIcon_Tap(sender: UITextFieldIcons) {
print("Delegate Method: You have tapped on the trailing icon");
}
}
| Attribute | Type | Default | Description |
|---|---|---|---|
| iconSpacingLeft | CGFloat | 8 | The spacing that appears to the left of the icon |
| iconSpacingRight | CGFloat | 8 | The spacing that appears to the right of the icon |
| leadingColor | UIColor | .placeholderText | The color of the leading icon |
| leadingIcon | UIImage | nil | The leading icon (as a UIImage) |
| leadingTapAction | Closure | nil | The action that occurs when the leading icon is tapped |
| trailingColor | UIColor | .placeholderText | The color of the trailing icon |
| trailingIcon | UIImage | nil | The trailing icon (as a UIImage) |
| trailingTapAction | Closure | nil | The action that occurs when the trailing icon is tapped |
- Currently you need to implment
UITextFieldDelegatealongsideUITextFieldIconsDelegatein your view controller