Multiline UILabel not working in Notification Content Extension - ios

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):
Also, when a new notification comes, I see a blank info and it's updated after a few secs.
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.

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
}

Related

Cannot set dynamic height based on text

I have a textview within which is a label. The label gets texts of varying lengths from the server.
Now the texts from the server are displayed like so on my label (which is in a textview)
In order to increase the textview height as per the text in the label, I did this...
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
incidentTextView.translatesAutoresizingMaskIntoConstraints = true
incidentTextView.sizeToFit()
actionTextView.translatesAutoresizingMaskIntoConstraints = true
actionTextView.sizeToFit()
submitByTextView.translatesAutoresizingMaskIntoConstraints = true
submitByTextView.sizeToFit()
}
But this gives me the result as shown below..
How can I align the fields like the first image and yet have the height of the textview increased dynamically as per the text in the label..?
EDIT 1: I cannot see the scrolling option also for UIView..
You could use UILabel instead UITextView and just set numberOfLines = 0.
Make sure you used correct constraints to align UILable proper.

iOS Swift round views inside cell show incorrectly

Evening, I have a calendar collection.
The cells have some rounded views shown incorrectly at the first time, but when they are reloaded they are shown correctly.
I know that the issue is that at the first time the cell doesn't know the right size of the frame.
What I've tried:
1- call the round function inside layoutSubviews(): only the right side is rounded correctly
2 - call the round function inside the cellWillLayout: nothing changes
This is the rounding function:
func makeRound() {
print("rounding")
currentDayView.layer.cornerRadius = currentDayView.frame.height/2
currentDayView.layer.masksToBounds = true
currentDayView.clipsToBounds = true
selectedDayView.layer.cornerRadius = selectedDayView.frame.height/2
selectedDayView.layer.masksToBounds = true
selectedDayView.clipsToBounds = true
}
Any suggestion?
The best place to do corner rounding is either in each view's layoutSubviews or (for example, if you haven't sub-classed them) put it in your view controllers viewDidLayoutSubviews.
Each view in your case is the layoutSubviews of currentDayView and selectedDayView.
You need to override layoutSubviews method, then call your method inside it:
override func layoutSubviews() {
super.layoutSubviews()
self.makeRound()
}

Why does sizeThatFits() return a size that is too small?

I'm learning swift with cs193p and I have a problem with UITextView.sizeThatFits(...). It should return a recommended size for popover view to display an [int] array as a text. As you can see in Paul Hegarty's example (https://youtu.be/gjl2gc70YHM?t=1h43m17s), he gets perfectly-fit popover window without scrollbar. I'm using almost the same code that was in this lecture, but instead i've got this:
the text string equals [100], but the sizeThatFits() method is returning a size that is too small to display it nicely, even though there is plenty of free space.
It is getting a bit better after I've added some text, but still not precise and with the scrollbar:
Here is the part of the code where the size is being set:
override var preferredContentSize: CGSize {
get {
if textView != nil && presentingViewController != nil {
// I've added these outputs so I can see the exact numbers to try to understand how this works
print("presentingViewController!.view.bounds.size = \(presentingViewController!.view.bounds.size)")
print("sizeThatFits = \(textView.sizeThatFits(presentingViewController!.view.bounds.size))")
return textView.sizeThatFits(presentingViewController!.view.bounds.size)
} else { return super.preferredContentSize }
}
set { super.preferredContentSize = newValue }
}
What should I do so this will work in the same way as in the lecture?
It looks like there are 16 pt margins between the label and its parent view. You need to take that into account when returning the preferred size of the popover.
You should try both of the following:
Add 32 to the width that's returned from preferredContentSize
In Interface Builder, clear the layout constraints on your UILabel, then re-add top, bottom, leading, and trailing constraints and make sure that "Constrain to Margins" option is not enabled.
Finally, instead of overriding preferredContentSize, you can simply set the preferredContentSize when your view is ready to display, and you can ask Auto Layout to choose the best size:
override func viewDidLayoutSubviews() {
self.preferredContentSize = self.view.systemLayoutSizeFitting(UILayoutFittingCompressedSize)
}
If your layout is configured correctly, systemLayoutSizeFitting(UILayoutFittingCompressedSize) will return the smallest possible size for your view, taking into account all of the margins and sub-views.

Unable to get the height of a UILabel with numberOfLines = 0

When I set a text to my label and check myLabel.frame.size.height I get always the same value, as if being always a single line, even if the text I set takes multiple lines... How could I get the "final" height?
Thanks
Try it inside your View Controller's viewDidLayoutSubviews function:
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
print(myLabel.frame.size.height)
}
Your label won't have it's final size until then.

Bounds and Frame deliver Wrong Value in iOS

I have a label in a View Controller scene inside a root view.
I set the constraint of the height to 30 using the Pins button right below.
Everything looks pretty in the storyboard.
But when I want to get the size of the element inside my code I always get the value 21. I tried refLabel.bounds.size.height and refLabel.frame.height. I call this in the override func viewDidLoad(){
super.viewDidLoad() function of the corresponding class.
Has this something to do with the intrinsic content size?
Try this code
refLabel.layoutIfNeeded()
let height = refLabel.frame.size.height
print("right height is \(height)")

Resources