3

I got two UITextfields with separator(UILabel) between them. I put them all into UIStackView.

While in editing mode, content of the textfield is cut from the top, as seen in the picture below

enter image description here

I've found that the only way to remove this issue is to make this separator big enough, but this spoils my design.

How to fix it?

It's worth to mention my UIStackView settings:

enter image description here

and show how I implement this custom bottomline-style UITextfield

class CustomTextField: UITextField {

override func awakeFromNib() {
    super.awakeFromNib()

    let attributedString = NSAttributedString(string: self.placeholder!, attributes: [NSForegroundColorAttributeName:UIColor.lightGray, NSFontAttributeName: UIFont(name: "GothamRounded-Book", size: 18.0)! ])
    self.attributedPlaceholder = attributedString
    self.tintColor = UIColor.appRed
    self.font = UIFont(name: "GothamRounded-Book", size: 18.0)!
    self.borderStyle = .none
    self.textAlignment = .center

}


override func textRect(forBounds bounds: CGRect) -> CGRect {
    return bounds.insetBy(dx: 0, dy: 5)
}

override func editingRect(forBounds bounds: CGRect) -> CGRect {
    return bounds.insetBy(dx: 0, dy: 5)
}


override var tintColor: UIColor! {

    didSet {
        setNeedsDisplay()
    }
}

override func draw(_ rect: CGRect) {

    let startingPoint   = CGPoint(x: rect.minX, y: rect.maxY)
    let endingPoint     = CGPoint(x: rect.maxX, y: rect.maxY)

    let path = UIBezierPath()

    path.move(to: startingPoint)
    path.addLine(to: endingPoint)
    path.lineWidth = 2.0

    tintColor.setStroke()
    tintColor = UIColor.appRed

    path.stroke()
}

}

Any help much appreciated

EDIT

I have another TextField like that and it works fine, but it doesn't sit inside any horizontal UIStackView. Here is the screenshot of hierarchy:

enter image description here

enter image description here

theDC
  • 6,364
  • 10
  • 56
  • 98

1 Answers1

0

Unfortunately you need to check the size on editing

class CustomTextField: UITextField {
   override func awakeFromNib() {
      super.awakeFromNib()
      self.addTarget(self, action: #selector(textFieldEditingChanged), for: .editingChanged)
   }

   func textFieldEditingChanged(_ textField: UITextField) {
      textField.invalidateIntrinsicContentSize()
   }


   override var intrinsicContentSize: CGSize {
      if isEditing {
         let string = text ?? ""
         let size = string.size(attributes: typingAttributes)
         return CGSize(width: size.width + (rightView?.bounds.size.width ?? 0) + (leftView?.bounds.size.width ?? 0) + 2,
                       height: size.height)
      }

      return super.intrinsicContentSize
   }
}
zombie
  • 5,069
  • 3
  • 25
  • 54