Nitesh
Nitesh

Reputation: 2024

Add text and image to UITextField right side

How do I add Text and Image on right of the UITextField. I have a UITextField which will initially have a text/label at right of it and when user click on the button the same textField will now have text + image to the right of UITextField

Image can be added like this but what about the text/label and both ?

txtField.rightViewMode = .always
txtField.rightView = UIImageView(image: UIImage(named: "selectdrop"))

Upvotes: 2

Views: 8445

Answers (1)

Nirav D
Nirav D

Reputation: 72410

For that you can create on UIView instance and add the UILabel and UIImageView inside it then finally set that view as rightView of textField. Some thing like below.

let rightView = UIView(frame: CGRect(x: 0, y: 0, width: 100, height: 40))

let label = UILabel(frame: CGRect(x: 0, y: 0, width: 60, height: 40))
label.text = "Text"
label.textColor = UIColor.red
label.textAlignment = .center

let imageView = UIImageView(frame: CGRect(x: 40, y: 0, width: 40, height: 40))
imageView.image = UIImage(named: "selectdrop")
rightView.addSubview(label)
rightView.addSubview(imageView)

txtField.rightView = rightView
txtField.rightViewMode = .always

Upvotes: 8

Related Questions