Skip to content
This repository has been archived by the owner on Jun 30, 2021. It is now read-only.

Commit

Permalink
Version 0.7.0
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
Luis Padron committed May 14, 2017
1 parent 2e58201 commit 9e50f4c
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 5 deletions.
7 changes: 6 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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 +
- iOS required of 9.0 +
2 changes: 1 addition & 1 deletion UIEmptyState.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
32 changes: 29 additions & 3 deletions UIEmptyState/UIEmptyStateView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 9e50f4c

Please sign in to comment.