Search code examples
iosswiftuser-interfaceuitextview

Text in UITextView goes out of its frames Swift 3


The problem is that when I enter some text in my textView the text goes out of its set frames. I am creating everything programatically though.

Creating UITextView:

    let descriptionTextView: UITextView = {
    let textView = UITextView()
    textView.backgroundColor = UIColor.customDarkGrayColor
    textView.text = "News Description"
    textView.textColor = UIColor.lightGray
    textView.font = UIFont.systemFont(ofSize: 15)
    textView.textContainerInset = UIEdgeInsets(top: 10, left: 16, bottom: 0, right: 16)
    textView.autocorrectionType = .no
    textView.setDefaultShadow()
    return textView
}()

Adding as subview and Setting Constraints using my custom method:

addSubview(descriptionTextView)

descriptionTextView.anchor(top: titleTextField.bottomAnchor, left: leftAnchor, bottom: nil, right: rightAnchor, paddingTop: 10, paddingLeft: 20, paddingBottom: 0, paddingRight: 20, width: 0, height: 80)

Check the bug here


Solution

  • A possible idea of where to look into:

    • Your UITextView is currently scrollable, so the text container has no limit on height
    • Since you add a shadow, you probably set masksToBounds or clipsToBounds to true which is why the text appears outside of the view frame.

    Let me know if that helps. Cheers!