Mohammed Nabil
Mohammed Nabil

Reputation: 157

Move cursor in uitextfield 3 space to the right

How to move and show cursor 3 spaces to the right in uitextfield when i click on uitextfield. I make uitextfield rounded corner and put constraints left "10" from main.storyboard and when i click on uitextfield the cursor shows outside of uitextfield. Below is the swift code.

@IBOutlet weak var txtSearch: UITextField!
override func viewDidLoad() {
        super.viewDidLoad()
        txtSearch.layer.cornerRadius = 22
        txtSearch.clipsToBounds = true
        txtSearch.layer.borderWidth = 2.0
        txtSearch.borderStyle = .none
}

Cursor outside of uitextfield

Upvotes: 0

Views: 86

Answers (1)

flanker
flanker

Reputation: 4200

To achieve this you'll need to create a custom UITextField by subclassing the standard object and then overrding the functions that set the boundary (i.e. inset) of the content. You'll need to work in points, not characters, so play with the inset variables to get the look you want.

class MyTextField: UITextField {
   var insetX: CGFloat = 20
   var insetY: CGFloat = 0
   
   override func textRect(forBounds bounds: CGRect) -> CGRect {
      return bounds.insetBy(dx: insetX , dy: insetY)
   }
   
   override func editingRect(forBounds bounds: CGRect) -> CGRect {
      return bounds.insetBy(dx: insetX , dy: insetY)
   }
}

You will need to set the class of the textfield in IB to be your custom class.

Upvotes: 1

Related Questions