Bhavuk Jain
Bhavuk Jain

Reputation: 2187

Multiline UILabel not working in Notification Content Extension

I've put a UILabel in my notification content extension storyboard and set it up using auto layout. I've also set the numberOfLines to 0. In the storyboard, it looks perfectly fine but when I use it on my device, the label is coming out to be a single line. Am I missing anything?

Edit (ScreenShots added):

enter image description here

enter image description here

Also, when a new notification comes, I see a blank info and it's updated after a few secs.

enter image description here

More ScreenShots Added:

Number of lines is set to 0.

and in the code I'm setting it like this:

titleString.text = "Saturday SuperBowl Saturday SuperBowl Saturday SuperBowl"

But still it is coming as a single line.

enter image description here

Upvotes: 1

Views: 393

Answers (1)

Robert Erdei
Robert Erdei

Reputation: 11

You must set the uilabels preferredMaxLayoutWidth. You can do this in your view controller:

override func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews()
    titleString.preferredMaxLayoutWidth = titleString.frame.size.width
    self.view.layoutIfNeeded()
}

or subclassing your uilabel:

override func layoutSubviews() {
    self.preferredMaxLayoutWidth = self.frame.size.width
    super.layoutSubviews
}

Upvotes: 1

Related Questions