0

For my UITextField, I want the border to be removed. So, I tried changing its border color to the background color (with border width of 1) but doing so, faint border lines are seen at the corners. Next, I set the border style to none in the attribute inspector. When I run the app, the border is gone. However, when the text is entered in it, the text gets cropped in the left side as shown in the image. I tried adding padding view to the textfield but it did not fix the issue. How can I solve this?

enter image description here

EDIT:

enter image description here The textfield is followed by a label. Since I want the label to follow the textfield content, I have not set the width of the textfield. This is shown in the image. When I add padding view with leftViewMode as always, the design in not rendered, and i get the console message:

- changing property masksToBounds in transform-only layer, will have no effect

Following one of the answers from @Surjeet's link, I tried extending the textField as:

class CustomTextField: UITextField {

    required init?(coder aDecoder: NSCoder){
        super.init(coder: aDecoder)
    }

    override func textRect(forBounds bounds: CGRect) -> CGRect {
        return CGRect(x: bounds.origin.x + 10, y: bounds.origin.y, width: bounds.size.width, height: bounds.size.height)
    }

    override func editingRect(forBounds bounds: CGRect) -> CGRect {
        return self.textRect(forBounds: bounds)
    }
}

But still the problem is not solved.

Sujal
  • 1,447
  • 19
  • 34
  • How you add padding into UITextField ? Some issue with that, It should be fixed by left padding. – Surjeet Singh Feb 06 '18 at 04:11
  • let paddingView = UIView(frame: CGRect(x: 0, y: 0, width: 35, height: self.myTextField.frame.height)) myTextField.leftView = paddingView Is this correct? – Sujal Feb 06 '18 at 04:13
  • Check [this link](https://stackoverflow.com/questions/3727068/set-padding-for-uitextfield-with-uitextborderstylenone), One addition property **leftViewMode **. try with this. – Surjeet Singh Feb 06 '18 at 04:17
  • Can u share, what you have tried till now? – Fahadsk Feb 06 '18 at 05:18

2 Answers2

0

You can add left padding for your textfield and your issue should be resolved.

Swift 4

        let paddingView: UIView = UIView.init(frame: CGRect(x: 0, y: 0, width: 5, height: 20))
        textField.leftView = paddingView
        textField.leftViewMode = .always
0
text_field.setLeftPaddingPoints(10)

text_field.setRightPaddingPoints(10)


extension UITextField {

    func setLeftPaddingPoints(_ amount:CGFloat){
       let paddingView = UIView(frame: CGRect(x: 0, y: 0, width: amount, height: self.frame.size.height))
       self.leftView = paddingView
       self.leftViewMode = .always
    }
    func setRightPaddingPoints(_ amount:CGFloat) {
      let paddingView = UIView(frame: CGRect(x: 0, y: 0, width: amount, height: self.frame.size.height))
      self.rightView = paddingView
      self.rightViewMode = .always
    }
}
Talha Ahmed
  • 160
  • 1
  • 10
  • 2
    While what you have written may answer the question, however it does seem a little lacking in [explanation](https://meta.stackexchange.com/questions/114762/explaining-entirely-code-based-answers) and may illicit confusion to other users. Can you please expand upon your answer so that it is clearer and more accessible? This will make for better answers and help future users understand how the problem was solved. – Andrew Nov 17 '19 at 20:42