Almost all the answers seem to update the ItemsViewController cellForRowAt method.
I have figured out a way to update the label colors in the ItemCell itself.
This uses a number formatter to get the value out of the currency string.
class ItemCell: UITableViewCell {
@IBOutlet var nameLabel: UILabel!
@IBOutlet var serialNumberLabel: UILabel!
@IBOutlet var valueLabel: ValueLabel!
override func awakeFromNib() {
super.awakeFromNib()
// Also available in Attributes Inspector > Dynamic Type > Automatically Adjusts Font
nameLabel.adjustsFontForContentSizeCategory = true
serialNumberLabel.adjustsFontForContentSizeCategory = true
valueLabel.adjustsFontForContentSizeCategory = true
}
}
class ValueLabel: UILabel {
override var text: String? {
didSet {
if let validText = text {
let formatter = NumberFormatter()
formatter.numberStyle = .currency
formatter.locale = Locale(identifier: "en_us")
if let value = formatter.number(from: validText) {
let amount = value.intValue
if amount >= 50 {
textColor = UIColor.red
}
else if amount < 50 {
textColor = UIColor.green
}
}
else {
textColor = UIColor.darkText
}
}
}
}
}
I like the solution of making changes from within ItemCell.swift. I went with the first solution that came to mind. A friend showed me how to use “Color Literals,” which I think are pretty cool: