Skip to content

Instantly share code, notes, and snippets.

@pravishanth
Created November 9, 2018 14:28
Show Gist options
  • Save pravishanth/14cdb8bf14dd8899b081bdc97988b985 to your computer and use it in GitHub Desktop.
Save pravishanth/14cdb8bf14dd8899b081bdc97988b985 to your computer and use it in GitHub Desktop.
TableViewCell with ImageView
class ProfileImageLabelTVCell: UITableViewCell {
let cellTextLabel : UILabel = {
let label = UILabel()
label.text = "Name"
return label
}()
let cellImageView : RoundedImageView = {
let iv = RoundedImageView()
iv.image = #imageLiteral(resourceName: "user_male_circle")
let imageWidthAndHeight :CGFloat = 50.0
iv.layer.masksToBounds = true
iv.heightAnchor.constraint(equalToConstant: imageWidthAndHeight).isActive = true
iv.widthAnchor.constraint(equalToConstant: imageWidthAndHeight).isActive = true
//iv.layoutSubviews()
//iv.layer.cornerRadius = imageWidthAndHeight / 2
return iv
}()
let imageNameSV = UIStackView() // Horizontal Center
override func awakeFromNib() {
super.awakeFromNib()
}
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)!
}
override init(style: UITableViewCellStyle, reuseIdentifier: String!) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
// Initialization code
let spacing1 : CGFloat = 20
// imageNameSV Stack view
imageNameSV.axis = UILayoutConstraintAxis.horizontal
imageNameSV.distribution = UIStackViewDistribution.equalSpacing
imageNameSV.alignment = UIStackViewAlignment.center
imageNameSV.spacing = spacing1
imageNameSV.addArrangedSubview(cellImageView)
imageNameSV.addArrangedSubview(cellTextLabel)
imageNameSV.translatesAutoresizingMaskIntoConstraints = false
contentView.addSubview(imageNameSV)
let viewsDict = [
"imageNameSV" : imageNameSV
]
// PatientDetailsSV Constraints
contentView.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|-3-[imageNameSV]-3-|", options: [], metrics: nil, views: viewsDict))
contentView.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|-10-[imageNameSV]", options: [], metrics: nil, views: viewsDict))
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
}
class RoundedImageView: UIImageView {
private weak var borderLayer: CAShapeLayer?
override func layoutSubviews() {
super.layoutSubviews()
// create path
let width = min(bounds.width, bounds.height)
let path = UIBezierPath(arcCenter: CGPoint(x: bounds.midX, y: bounds.midY), radius: width / 2, startAngle: 0, endAngle: .pi * 2, clockwise: true)
// update mask and save for future reference
let mask = CAShapeLayer()
mask.path = path.cgPath
layer.mask = mask
// create border layer
let frameLayer = CAShapeLayer()
frameLayer.path = path.cgPath
//frameLayer.lineWidth = 10.0
frameLayer.lineWidth = 0
frameLayer.strokeColor = UIColor.white.cgColor
frameLayer.fillColor = nil
borderLayer?.removeFromSuperlayer()
layer.addSublayer(frameLayer)
borderLayer = frameLayer
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment