diff --git a/CHANGELOG.md b/CHANGELOG.md index 6742210..5bb6067 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,10 @@ # UIEmptyState Changelog +## Version 0.7.0 + +- Update constraints for labels so that they do not extend past the edges of the view +- Add private extension to help calculate the the height of labels + ## Version 0.6.0 - Fix bug where title for UIEmptyState was not being updated when reloading @@ -82,4 +87,4 @@ Due to the change from methods to properties, the way you interact with the data - Initial release - Currently only works with `UITableViewController` -- iOS required of 9.0 + \ No newline at end of file +- iOS required of 9.0 + diff --git a/UIEmptyState.podspec b/UIEmptyState.podspec index c36bb39..1a889d2 100644 --- a/UIEmptyState.podspec +++ b/UIEmptyState.podspec @@ -2,7 +2,7 @@ Pod::Spec.new do |s| s.name = "UIEmptyState" - s.version = "0.6.0" + s.version = "0.7.0" s.summary = "An empty state control to give visually appealing context when building iOS applications." s.description = <<-DESC Empty state control which gives context with either a message, image, and buttons to the user when ever there is a reason the state is empty. diff --git a/UIEmptyState/UIEmptyStateView.swift b/UIEmptyState/UIEmptyStateView.swift index 1e74e6f..7c96500 100644 --- a/UIEmptyState/UIEmptyStateView.swift +++ b/UIEmptyState/UIEmptyStateView.swift @@ -8,6 +8,24 @@ import UIKit +private extension UILabel { + /// Returns the height that would be expected for the string, with a max width + func expectedHeight(forWidth width: CGFloat) -> CGFloat { + guard let txt = self.text else { + return 0.0 + } + + let maxSize = CGSize(width: width, height: .greatestFiniteMagnitude) + + let attrString = NSAttributedString(string: txt, attributes: [NSFontAttributeName : self.font]) + let expectedRect = attrString.boundingRect(with: maxSize, + options: .usesLineFragmentOrigin, + context: nil) + return ceil(expectedRect.size.height) + + } +} + /// A UIView which has a stack view and inside the stackview are 1-4 other views /// This view is used as the default view for the `emptyStateView` in the `UIEmptyStateDataSource` open class UIEmptyStateView: UIView { @@ -141,9 +159,17 @@ open class UIEmptyStateView: UIView { for subview in contentView.subviews { subview.removeConstraints(subview.constraints) if let label = subview as? UILabel { - label.sizeToFit() - label.widthAnchor.constraint(equalToConstant: label.frame.width).isActive = true - label.heightAnchor.constraint(equalToConstant: label.frame.height).isActive = true + + if let maxWidth = contentView.superview?.readableContentGuide.layoutFrame.width { + label.widthAnchor.constraint(equalToConstant: maxWidth).isActive = true + label.heightAnchor.constraint(equalToConstant: + label.expectedHeight(forWidth: maxWidth)).isActive = true + } else { + label.sizeToFit() + label.widthAnchor.constraint(equalToConstant: label.frame.width).isActive = true + label.heightAnchor.constraint(equalToConstant: label.frame.height).isActive = true + } + } else if let imageView = subview as? UIImageView { let size = imageSize ?? CGSize(width: 100, height: 100) imageView.heightAnchor.constraint(equalToConstant: size.height).isActive = true