Reputation: 81
I've constrained my UILabel to constant width using NSLayoutConstraint
and widthAnchor
, the problem is, the contentSize
of the UILabel
is not equal to the widthAnchor
and in some cases is greater than what I specified.
Why is that the case? P.S. I'm a beginner to iOS Development, apologies if the answer is obvious.
Edit: The UILabel's width was constrained to a decimal point value, which according to one user on another forum caused the UILabel to round up to the next 0.5 multiple, this caused the whole system to break. The work around for me was ceil() the width of my UILabel. Thanks to everyone who helped.
Upvotes: 0
Views: 396
Reputation: 81
The UILabel's width was constrained to a decimal point value, which according to one user on another forum caused the UILabel to round up to the next 0.5 multiple, this caused the whole system to break. The work around for me was ceil() the width of my UILabel. Thanks to everyone who helped.
Upvotes: 0
Reputation: 5098
You can use the compression resistance priority here,
WidthAnchor
check code below.
label.setContentCompressionResistancePriority(UILayoutPriority(rawValue: 1000), for: .horizontal)
This article might be helpful in your case.
Upvotes: 1