Created
April 15, 2020 15:31
-
-
Save karthikgs7/6d94d607733aa40c557efcfcc527f45d to your computer and use it in GitHub Desktop.
Simple UITableViewCell subclass with image and text. Resizes w.r.t. content.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public final class ImageTextTableViewCell: UITableViewCell { | |
private var _imageView: UIImageView! | |
private var _label: UILabel! | |
} | |
private extension ImageTextTableViewCell { | |
func setup() { | |
setupImageView() | |
setupLabel() | |
} | |
func setupImageView() { | |
_imageView = UIImageView() | |
addSubview(_imageView) | |
_imageView?.translatesAutoresizingMaskIntoConstraints = false | |
let topConstraint = _imageView.topAnchor.constraint(greaterThanOrEqualTo: topAnchor, constant: 20.0) | |
topConstraint.priority = UILayoutPriority.init(rawValue: 998) | |
let bottomConstraint = _imageView.bottomAnchor.constraint(greaterThanOrEqualTo: bottomAnchor, constant: -20.0) | |
bottomConstraint.priority = UILayoutPriority.init(rawValue: 998) | |
addConstraints([ | |
_imageView.centerYAnchor.constraint(equalTo: centerYAnchor), | |
_imageView.leadingAnchor.constraint(equalTo: safeAreaLayoutGuide.leadingAnchor, constant: 8.0), | |
topConstraint, | |
bottomConstraint | |
]) | |
_imageView.contentMode = .scaleAspectFit | |
_imageView.clipsToBounds = true | |
_imageView.setContentHuggingPriority(.required, for: .horizontal) | |
_imageView.setContentHuggingPriority(.required, for: .vertical) | |
_imageView.setContentCompressionResistancePriority(.required, for: .horizontal) | |
_imageView.setContentCompressionResistancePriority(.required, for: .vertical) | |
} | |
func setupLabel() { | |
_label = UILabel() | |
addSubview(_label) | |
_label.translatesAutoresizingMaskIntoConstraints = false | |
let layoutXAxisAnchor: NSLayoutXAxisAnchor = safeAreaLayoutGuide.trailingAnchor | |
let constant: CGFloat = (accessoryType != .none) ? -45.0 : -20.0 | |
let trailingConstraint = _label.trailingAnchor.constraint(lessThanOrEqualTo: layoutXAxisAnchor, constant: constant) | |
let topConstraint = _label.topAnchor.constraint(greaterThanOrEqualTo: topAnchor, constant: 20.0) | |
topConstraint.priority = UILayoutPriority.init(rawValue: 999) | |
let bottomConstraint = _label.bottomAnchor.constraint(greaterThanOrEqualTo: bottomAnchor, constant: -20.0) | |
bottomConstraint.priority = UILayoutPriority.init(rawValue: 999) | |
addConstraints([ | |
_label.leadingAnchor.constraint(equalTo: _imageView.trailingAnchor, constant: 8.0), | |
_label.centerYAnchor.constraint(equalTo: centerYAnchor), | |
topConstraint, | |
bottomConstraint, | |
trailingConstraint | |
]) | |
_label.numberOfLines = 0 | |
_label.text = "Sample Text..." | |
_label.setContentHuggingPriority(.required, for: .horizontal) | |
_label.setContentCompressionResistancePriority(.required, for: .vertical) | |
} | |
} | |
extension ImageTextTableViewCell { | |
public func configureWithImage(_ image: UIImage, text: String) { | |
setup() | |
_imageView.image = image | |
_label.text = text | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment