Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add icon or image in UITextField on left / right in Swift 4

I have set up the left icon in UITextField. When I set text, it is over the left icon. I want to set text after the icon in the UITextField. I have used below code.

    let imageView = UIImageView(image: UIImage(named: strImgname))
    imageView.frame = CGRect(x: 0, y: 0, width: imageView.image!.size.width , height: imageView.image!.size.height)
    let paddingView: UIView = UIView.init(frame: CGRect(x: 0, y: 0, width: 50, height: 30))        
    paddingView.addSubview(imageView)
    txtField.leftViewMode = .always
    txtField.leftView = paddingView
like image 841
Enamul Haque Avatar asked Aug 14 '18 12:08

Enamul Haque


People also ask

How do I limit characters in UITextField Swift?

If you have a UITextField or UITextView and want to stop users typing in more than a certain number of letters, you need to set yourself as the delegate for the control then implement either shouldChangeCharactersIn (for text fields) or shouldChangeTextIn (for text views).

How do I assign an image in Swift?

First one is to drag and drop an image file from Finder onto the assets catalog. Dragging the image will automatically create new image set for you. Drag and drop image onto Xcode's assets catalog. Or, click on a plus button at the very bottom of the Assets navigator view and then select “New Image Set”.


1 Answers

You can use @IBDesignable to make a designable UITextField with these capabilities and even more and use it through out your project.

DesignableTextField with Delegate methods when icon in UITextField is tapped:

import Foundation
import UIKit

protocol DesignableTextFieldDelegate: UITextFieldDelegate {
    func textFieldIconClicked(btn:UIButton)
}

@IBDesignable
class DesignableTextField: UITextField {

    //Delegate when image/icon is tapped.
    private var myDelegate: DesignableTextFieldDelegate? {
        get { return delegate as? DesignableTextFieldDelegate }
    }

    @objc func buttonClicked(btn: UIButton){
        self.myDelegate?.textFieldIconClicked(btn: btn)
    }

    //Padding images on left
    override func leftViewRect(forBounds bounds: CGRect) -> CGRect {
        var textRect = super.leftViewRect(forBounds: bounds)
        textRect.origin.x += padding
        return textRect
    }

    //Padding images on Right
    override func rightViewRect(forBounds bounds: CGRect) -> CGRect {
        var textRect = super.rightViewRect(forBounds: bounds)
        textRect.origin.x -= padding
        return textRect
    }

    @IBInspectable var padding: CGFloat = 0
    @IBInspectable var leadingImage: UIImage? { didSet { updateView() }}
    @IBInspectable var color: UIColor = UIColor.lightGray { didSet { updateView() }}
    @IBInspectable var imageColor: UIColor = UIColor.init(hex: "3EB2FF") { didSet { updateView() }}
    @IBInspectable var rtl: Bool = false { didSet { updateView() }}

    func updateView() {
        rightViewMode = UITextFieldViewMode.never
        rightView = nil
        leftViewMode = UITextFieldViewMode.never
        leftView = nil

        if let image = leadingImage {
            let button = UIButton(type: .custom)
            button.frame = CGRect(x: 0, y: 0, width: 20, height: 20)

            let tintedImage = image.withRenderingMode(.alwaysTemplate)
            button.setImage(tintedImage, for: .normal)
            button.tintColor = imageColor

            button.setTitleColor(UIColor.clear, for: .normal)
            button.addTarget(self, action: #selector(buttonClicked(btn:)), for: UIControlEvents.touchDown)
            button.isUserInteractionEnabled = true

            if rtl {
                rightViewMode = UITextFieldViewMode.always
                rightView = button
            } else {
                leftViewMode = UITextFieldViewMode.always
                leftView = button
            }
        }

        // Placeholder text color
        attributedPlaceholder = NSAttributedString(string: placeholder != nil ?  placeholder! : "", attributes:[NSAttributedStringKey.foregroundColor: color])
    }
}

Now its Designable in the Storyboard as follows:

enter image description here NOTE: Rtl when set to Off icon will move to left of UITextField

Conforming the Delegate in desired ViewController.

class MyViewController: UIViewController, DesignableTextFieldDelegate {
    @IBOutlet weak var txtFieldSomeSearch: DesignableTextField!
    txtFieldSomeSearch.delegate = self // can be done in storyboard as well
    ... // other codes
    func textFieldIconClicked(btn: UIButton) {
        print("MyViewController : textFieldIconClicked")
    }
    ... // other codes
}

Finally Output :

enter image description here

like image 177
Rizwan Avatar answered Oct 06 '22 03:10

Rizwan