Skip to content

Instantly share code, notes, and snippets.

@yzhong52
Last active February 16, 2020 22:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yzhong52/7d169a2a40c88bf766517a00ce8a8129 to your computer and use it in GitHub Desktop.
Save yzhong52/7d169a2a40c88bf766517a00ce8a8129 to your computer and use it in GitHub Desktop.
Building a Client App From Scratch (NewsTableViewCell)
import UIKit
class NewsTableViewCell: UITableViewCell {
let titleLabel: UILabel = {
let lable = UILabel()
lable.translatesAutoresizingMaskIntoConstraints = false
lable.numberOfLines = 3
lable.font = UIFont.systemFont(ofSize: 14)
return lable
}()
let contentLabel: UILabel = {
let lable = UILabel()
lable.translatesAutoresizingMaskIntoConstraints = false
lable.numberOfLines = 4
lable.textColor = .darkGray
lable.font = UIFont.systemFont(ofSize: 12)
return lable
}()
let thumbnailView: UIImageView = {
let imageView = UIImageView()
imageView.translatesAutoresizingMaskIntoConstraints = false
imageView.contentMode = .scaleAspectFit
NSLayoutConstraint.activate([
imageView.widthAnchor.constraint(equalToConstant: 80),
])
return imageView
}()
private let horizontalStackView: UIStackView = {
let stackView = UIStackView()
stackView.translatesAutoresizingMaskIntoConstraints = false
stackView.axis = .horizontal
stackView.spacing = 12
stackView.alignment = .center
return stackView
}()
private let rightVerticalStackView: UIStackView = {
let stackView = UIStackView()
stackView.translatesAutoresizingMaskIntoConstraints = false
stackView.axis = .vertical
stackView.spacing = 4
return stackView
}()
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
addSubview(horizontalStackView)
let padding: CGFloat = 16
NSLayoutConstraint.activate([
horizontalStackView.topAnchor.constraint(equalTo: topAnchor, constant: padding),
horizontalStackView.bottomAnchor.constraint(equalTo: bottomAnchor, constant: -padding),
horizontalStackView.leftAnchor.constraint(equalTo: leftAnchor, constant: padding),
horizontalStackView.rightAnchor.constraint(equalTo: rightAnchor, constant: -padding),
])
horizontalStackView.addArrangedSubview(thumbnailView)
rightVerticalStackView.addArrangedSubview(titleLabel)
rightVerticalStackView.addArrangedSubview(contentLabel)
horizontalStackView.addArrangedSubview(rightVerticalStackView)
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment