0

I check out this answer, helpful, but didn't get what I want: Getting and Setting Cursor Position of UITextField and UITextView in Swift.

In the attribute inspector, I set Placeholder to " ____ First Name", and it fixes the left alignment of the text since it is moved to the right a bit. But I don't think that is the proper way to do it. Also, I didn't quite get the cursor position explanation from the above answer.

     func textView(_ textView: UITextView, shouldInteractWith URL: URL, in characterRange: NSRange, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool
        {
            let arbitraryValue: Int = 5
            if let newPosition = textView.position(from: textView.beginningOfDocument, offset: arbitraryValue) {
                textView.selectedTextRange = textView.textRange(from: newPosition, to: newPosition)

            // ....

            }
        }

enter image description here

The thing is even after somehow I figured it out to make it work, the cursor is moved 5 characters to right. When I removed the text, the cursor moves back into the left corner.

But what I exactly want is the whole textField to move to the right so that it doesn't get covered with CornerRadius like above.

2 Answers2

2

You can change the padding on the text field itself.

Swift:

 //Changes the text padding
 override func textRect(forBounds bounds: CGRect) -> CGRect {
   return UIEdgeInsetsInsetRect(bounds, UIEdgeInsetsMake(0, 15, 0, 15))
 }
 //Changes the placeholder
 override func placeholderRect(forBounds bounds: CGRect) -> CGRect {
   return UIEdgeInsetsInsetRect(bounds, UIEdgeInsetsMake(0, 15, 0, 15))
 }
Yuri Frota
  • 31
  • 4
  • Why are you using override on function? It will give you error "Method does not override any method from its superclass". – ShadeToD Sep 11 '19 at 07:47
0

So here is what I did without adding any codes.

  • I placed the Views underneath the textFields.
  • I made the Views make extra width on both sides.
  • I set the Views color that of textFields.
  • Then I give them corner radius property.

enter image description here

And if we look at it closely, we can see those edges. They make me cringe.

enter image description here