Last active
November 25, 2017 20:50
-
-
Save raulriera/31ecd8154abd216c52cfd6bee76bc6c4 to your computer and use it in GitHub Desktop.
HostCell implementation of a UILabel for the Building apps with FunctionalTableData series
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
typealias LabelCell = HostCell<UILabel, LabelState, LayoutMarginsTableItemLayout> | |
struct LabelState: Equatable { | |
let text: String | |
let font: UIFont | |
let isMultiline: Bool | |
init(text: String, font: UIFont = UIFont.systemFont(ofSize: 17), isMultiline: Bool = true) { | |
self.text = text | |
self.font = font | |
self.isMultiline = isMultiline | |
} | |
// Use this function to update the state of the view. In this case, | |
// updating the content of the `UILabel` with the passed state. | |
// If the state is `nil`, this cell is about to be reused by `UITableView` | |
// so prepare it to be reused. | |
static func updateView(_ view: UILabel, state: LabelState?) { | |
guard let state = state else { | |
view.text = nil | |
view.font = UIFont.systemFont(ofSize: 17) | |
view.numberOfLines = 1 | |
view.lineBreakMode = .byTruncatingTail | |
return | |
} | |
view.text = state.text | |
view.font = state.font | |
view.textColor = UIColor.body | |
if state.isMultiline { | |
view.numberOfLines = 0 | |
view.lineBreakMode = .byWordWrapping | |
} else { | |
view.numberOfLines = 1 | |
view.lineBreakMode = .byTruncatingTail | |
} | |
} | |
static func ==(lhs: LabelState, rhs: LabelState) -> Bool { | |
return lhs.text == rhs.text && lhs.font == rhs.font && lhs.isMultiline == rhs.isMultiline | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment