|
// |
|
// FeedTableViewCell.swift |
|
// Created by Damion Nabarrete on 1/18/16. |
|
// |
|
|
|
import UIKit |
|
import PureLayout |
|
|
|
|
|
class FeedTableViewCell: UITableViewCell { |
|
|
|
private var hasBeenViewed: Bool! |
|
private var timeWrap: UIView! |
|
private var separatorWrap: UIView! |
|
private var separator: UIView! |
|
private var separatorWidthConstraint: NSLayoutConstraint! |
|
private var subtitleWrap: UIView! |
|
private var addEventButton: UIButton! |
|
var titleLabel: UILabel! |
|
var descriptionLabel: UILabel! |
|
var timeLabel: UILabel! |
|
var timePeriodLabel: UILabel! |
|
|
|
|
|
init() { |
|
super.init(style: UITableViewCellStyle.Default, reuseIdentifier: "FeedCell") |
|
} |
|
|
|
required init?(coder aDecoder: NSCoder) { |
|
super.init(coder: aDecoder) |
|
} |
|
|
|
|
|
// MARK: - Cell methods |
|
|
|
func setAsViewed() { |
|
hasBeenViewed = true |
|
separatorWidthConstraint.constant = 1 |
|
separator.backgroundColor = UIColor.groupTableViewBackgroundColor() |
|
} |
|
|
|
|
|
// MARK: - Setup |
|
|
|
private func setupViews(eventType: String) { |
|
hasBeenViewed = false |
|
|
|
timeWrap = UIView.newAutoLayoutView() |
|
timeLabel = UILabel.newAutoLayoutView() |
|
timeLabel.textColor = UIColor.customPlaceholderColor() |
|
timeLabel.font = UIFont.systemFontOfSize(11) |
|
timeLabel.textAlignment = .Right |
|
timePeriodLabel = UILabel.newAutoLayoutView() |
|
timePeriodLabel.textColor = UIColor.customPlaceholderColor() |
|
timePeriodLabel.font = UIFont.systemFontOfSize(11) |
|
timePeriodLabel.textAlignment = .Right |
|
|
|
separatorWrap = UIView.newAutoLayoutView() |
|
separator = UIView.newAutoLayoutView() |
|
separator.backgroundColor = UIColor.customGreenColor() |
|
|
|
titleLabel = UILabel.newAutoLayoutView() |
|
titleLabel.preferredMaxLayoutWidth = 280 |
|
titleLabel.numberOfLines = 0 |
|
titleLabel.lineBreakMode = .ByWordWrapping |
|
titleLabel.textColor = UIColor.customTextColor() |
|
|
|
subtitleWrap = UIView.newAutoLayoutView() |
|
|
|
descriptionLabel = UILabel.newAutoLayoutView() |
|
descriptionLabel.preferredMaxLayoutWidth = 280 |
|
descriptionLabel.numberOfLines = 0 |
|
descriptionLabel.lineBreakMode = .ByWordWrapping |
|
descriptionLabel.font = UIFont.systemFontOfSize(11) |
|
descriptionLabel.textColor = UIColor.customPlaceholderColor() |
|
|
|
addEventButton = UIButton.newAutoLayoutView() |
|
addEventButton.setTitle("+", forState: UIControlState.Normal) |
|
addEventButton.setTitleColor(UIColor.groupTableViewBackgroundColor(), forState: .Normal) |
|
if #available(iOS 8.2, *) { |
|
addEventButton.titleLabel!.font = UIFont.systemFontOfSize(24, weight: UIFontWeightLight) |
|
} else { |
|
addEventButton.titleLabel!.font = UIFont.systemFontOfSize(24) |
|
} |
|
|
|
|
|
var views: [String : UIView] = [ |
|
"timeWrap": timeWrap, |
|
"titleLabel": titleLabel, |
|
"descriptionLabel": descriptionLabel, |
|
"separatorWrap": separatorWrap, |
|
"addEventButton": addEventButton, |
|
] |
|
|
|
timeWrap.addSubview(timeLabel) |
|
timeWrap.addSubview(timePeriodLabel) |
|
separatorWrap.addSubview(separator) |
|
subtitleWrap.addSubview(descriptionLabel) |
|
|
|
for (_, view) in views { |
|
contentView.addSubview(view) |
|
} |
|
} |
|
|
|
var didSetupConstraints = false |
|
override func updateConstraints() { |
|
|
|
if !didSetupConstraints { |
|
let xPadding: CGFloat = 5 |
|
let xPaddingPlus: CGFloat = 7 |
|
let xTimeWrapWidth: CGFloat = 36 |
|
let xTimeInset: CGFloat = 16 |
|
let xTitleInset: CGFloat = 10 |
|
|
|
timeWrap.autoSetDimension(.Width, toSize: xTimeWrapWidth) |
|
timeWrap.autoPinEdgeToSuperviewEdge(.Leading) |
|
timeWrap.autoPinEdgeToSuperviewEdge(.Top, withInset: xTimeInset) |
|
|
|
timeLabel.autoPinEdgeToSuperviewEdge(.Trailing) |
|
timeLabel.autoPinEdgeToSuperviewEdge(.Top) |
|
|
|
timePeriodLabel.autoPinEdgeToSuperviewEdge(.Trailing) |
|
timePeriodLabel.autoPinEdge(.Top, toEdge: .Bottom, ofView: timeLabel, withOffset: xPaddingPlus) |
|
|
|
separatorWrap.autoSetDimension(.Width, toSize: 2) |
|
separatorWrap.autoPinEdge(.Leading, toEdge: .Trailing, ofView: timeWrap, withOffset: xPaddingPlus) |
|
separatorWrap.autoPinEdgeToSuperviewEdge(.Top, withInset: xPadding) |
|
separatorWrap.autoPinEdgeToSuperviewEdge(.Bottom, withInset: xPadding) |
|
|
|
separatorWidthConstraint = separator.autoSetDimension(.Width, toSize: 2) |
|
separator.autoPinEdgeToSuperviewEdge(.Leading) |
|
separator.autoPinEdgeToSuperviewEdge(.Top) |
|
separator.autoPinEdgeToSuperviewEdge(.Bottom) |
|
|
|
// I ended up not needing compression resistance set |
|
// NSLayoutConstraint.autoSetPriority(UILayoutPriorityRequired) { |
|
// self.titleLabel.autoSetContentCompressionResistancePriorityForAxis(.Vertical) |
|
// self.descriptionLabel.autoSetContentCompressionResistancePriorityForAxis(.Vertical) |
|
// } |
|
|
|
titleLabel.autoPinEdge(.Leading, toEdge: .Trailing, ofView: separatorWrap, withOffset: xPadding) |
|
titleLabel.autoPinEdgeToSuperviewEdge(.Top, withInset: xTitleInset) |
|
|
|
descriptionLabel.autoPinEdge(.Top, toEdge: .Bottom, ofView: titleLabel, withOffset: 6) |
|
|
|
descriptionLabel.autoPinEdge(.Leading, toEdge: .Trailing, ofView: separatorWrap, withOffset: xPadding) |
|
descriptionLabel.autoPinEdgeToSuperviewEdge(.Bottom, withInset: xTitleInset) |
|
|
|
addEventButton.autoPinEdgeToSuperviewEdge(.Trailing) |
|
addEventButton.autoPinEdgeToSuperviewEdge(.Top, withInset: -2) |
|
|
|
didSetupConstraints = true |
|
} |
|
|
|
super.updateConstraints() |
|
} |
|
|
|
} |
If anyone knows how to fix xcode's ugly indentation, I'd love to hear it.