user12388503
user12388503

Reputation:

Swift - constrain UITextView

I have a UITextView and I would like to constrain it the same way as I would a UILabel. But if I use the same constrains as I would with a UILabel I am getting a different result. I also do not really quite understand how UITextView.frame works because it doesn't really matter what I set height/width, the result stays the same.

In the picture below "LinkTest" is my UITextView. As you can see it is not lined up with the UILabels below even though I constrain it the same way.

enter image description here

UITextView:

    let linkLabel: UITextView = {
        let v = UITextView()
        v.backgroundColor = .clear
        v.text = "Link"
        v.textColor = .lightGray
        v.font = UIFont(name: "AvenirNext-Regular", size: 18)
        v.textAlignment = .right
        v.isSelectable = false
        v.isScrollEnabled = false
        v.frame = CGRect(x: 0, y: 0, width: 20, height: 20)
//        v.attributedText = NSAttributedString(string: "", attributes: [.underlineStyle: NSUnderlineStyle.single.rawValue])
        v.translatesAutoresizingMaskIntoConstraints = false
        return v
    }()

Constrains:

// constrain linkLabel
    linkLabel.topAnchor.constraint(equalTo: linkImage.topAnchor).isActive = true
    linkLabel.leadingAnchor.constraint(equalTo: linkImage.leadingAnchor, constant: 30).isActive = true
    linkLabel.trailingAnchor.constraint(equalTo: contentView.trailingAnchor, constant: -20).isActive = true

    // constrain priceLabel
    priceLabel.topAnchor.constraint(equalTo: linkLabel.topAnchor, constant: 35).isActive = true
    priceLabel.leadingAnchor.constraint(equalTo: linkImage.leadingAnchor, constant: 30).isActive = true
    priceLabel.trailingAnchor.constraint(equalTo: contentView.trailingAnchor, constant: -20).isActive = true

    // constrain noteLabel
    noteLabel.topAnchor.constraint(equalTo: priceLabel.topAnchor, constant: 35).isActive = true
    noteLabel.leadingAnchor.constraint(equalTo: linkImage.leadingAnchor, constant: 30).isActive = true
    noteLabel.trailingAnchor.constraint(equalTo: contentView.trailingAnchor, constant: -20).isActive = true

I appreciate any help on this :)

Upvotes: 1

Views: 292

Answers (3)

user12388503
user12388503

Reputation:

Solution:

The problem was the UITextViews padding. Removing all padding solved the problem:

let padding = v.textContainer.lineFragmentPadding
 v.textContainerInset =  UIEdgeInsets(top: 0, left: -padding, bottom: 0, right: -padding)

Upvotes: 1

Kishan Bhatiya
Kishan Bhatiya

Reputation: 2378

You can set constraints using frame or using autolayout both can not work at same time. when you set v.translatesAutoresizingMaskIntoConstraints = false then frame setting doesn't affect and also you have to add height constraint

// constrain linkLabel
    linkLabel.topAnchor.constraint(equalTo: linkImage.topAnchor).isActive = true
    linkLabel.leadingAnchor.constraint(equalTo: linkImage.leadingAnchor, constant: 30).isActive = true
    linkLabel.trailingAnchor.constraint(equalTo: contentView.trailingAnchor, constant: -20).isActive = true
    linkLabel.heightAnchor.constraint(equalToConstant: 20).isActive = true

Upvotes: 0

Shehata Gamal
Shehata Gamal

Reputation: 100549

When you set

v.translatesAutoresizingMaskIntoConstraints = false

then frame setting is ignored , btw you need a height

linkLabel.heightAnchor.constraint(equalToConstant: 30).isActive = true

Upvotes: 0

Related Questions