-1

How can I make a UIlabel Wrap as many times as needed based on a comment text given to it?

I have tried many things like:

        self.commentText.contentMode = .scaleToFill
    self.commentText.numberOfLines = 0
//        self.commentText.leadingMargin(pixel: 10)
//        self.commentText.trailingMargin(pixel: 10)

Which I got from [Qs like.] and UILabel - auto-size label to fit text?1

But am not able to make the wrapping work

How I setup:

        private func setupTheCommentText() {
        self.commentText.frame = CGRect(x: 85, y: 35, width: 272, height: 15)
        self.commentText.textColor = UIColor(red: 92/255, green: 92/255, blue: 92/255, alpha: 1)
        self.commentText.font = UIFont(name: "SFCompactDisplay-Medium", size: 13)
        //self.commentText.numberOfLines = 0
        //self.commentText.contentMode = .scaleAspectFill
        //self.commentText.translatesAutoresizingMaskIntoConstraints = false
//        self.commentText.leadingMargin(pixel: 10)
//        self.commentText.trailingMargin(pixel: 10)
        self.addSubview(commentText)
        self.commentText.topAnchor.constraint(equalTo:username.bottomAnchor).isActive = true
        commentText.translatesAutoresizingMaskIntoConstraints = false

        NSLayoutConstraint.activate([
            self.commentText.topAnchor.constraint(equalTo:username.bottomAnchor),
            self.commentText.leadingAnchor.constraint(equalTo:self.contentView.leadingAnchor,constant:85.0),
            self.commentText.trailingAnchor.constraint(equalTo:self.contentView.trailingAnchor,constant:-15.0),//,constant:-85.0
            self.commentText.bottomAnchor.constraint(equalTo:commentText.bottomAnchor)
            ])
    }
  • Have you given the label a fixed width? Show the code you are using to add the label to the view. Or show a screenshot of the storyboard and constraints. – Fogmeister Jul 06 '19 at 21:26
  • From your edit you seem to be mixing up using frames and using auto layout constraints. You should use one or the other. You need to add the rest of the constraints to the view. – Fogmeister Jul 06 '19 at 21:29

1 Answers1

1

Give these constraints

leading,trailing,top,bottom // bottom is for cells

commentText.translatesAutoresizingMaskIntoConstraints = false

NSLayoutConstraint.activate([
  self.commentText.topAnchor.constraint(equalTo:username.bottomAnchor),
  self.commentText.leadingAnchor.constraint(equalTo:self.view.leadingAnchor,constant:85.0),
  self.commentText.trailingAnchor.constraint(equalTo:self.view.trailingAnchor,constant:-85.0),
  self.commentText.bottomAnchor.constraint(equalTo:self.view.bottomAnchor) // this for cells 
])

Above i assume it's inside a vc's view , if it's no the case and it's a cell replace self.view with self.contentView

Sh_Khan
  • 86,695
  • 6
  • 38
  • 57
  • I have tried many variations with this code and although the text is wrapping it now appears begging not x=85. how can I make it start from 85? –  Jul 06 '19 at 21:39
  • add `,constant:85.0` to the `leading/trailing` anchor see edit – Sh_Khan Jul 06 '19 at 21:41
  • To get closer to the intended result I did this: NSLayoutConstraint.activate([ self.commentText.topAnchor.constraint(equalTo:username.bottomAnchor), self.commentText.leadingAnchor.constraint(equalTo:self.leadingAnchor,constant:85.0), self.commentText.trailingAnchor.constraint(equalTo:self.trailingAnchor),//,constant:-85.0 //self.commentText.bottomAnchor.constraint(equalTo:self.bottomAnchor) // this for cells ]) –  Jul 06 '19 at 21:45
  • The issue is that how the label does not wrap –  Jul 06 '19 at 21:45
  • where it is table/collection ? – Sh_Khan Jul 06 '19 at 22:01
  • this code is inside the customTableView cell class, the tableView is inside a container which is inside a baseVC –  Jul 06 '19 at 22:05
  • Even if I do: self.contentView –  Jul 06 '19 at 22:24
  • Do `cell.layoutIfNeeded()` before `return cell` – Sh_Khan Jul 06 '19 at 23:14