Skip to content

Instantly share code, notes, and snippets.

@raulriera
Last active November 25, 2017 20:50
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 raulriera/31ecd8154abd216c52cfd6bee76bc6c4 to your computer and use it in GitHub Desktop.
Save raulriera/31ecd8154abd216c52cfd6bee76bc6c4 to your computer and use it in GitHub Desktop.
HostCell implementation of a UILabel for the Building apps with FunctionalTableData series
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