Merge pull request #367 from zhongwuzw/optimize-dateformatter

Optimize DateFormatter to static variable
This commit is contained in:
Steven Deutsch
2017-12-02 13:58:28 -06:00
committed by GitHub
@@ -220,14 +220,21 @@ extension ConversationViewController: MessagesDataSource {
func cellTopLabelAttributedText(for message: MessageType, at indexPath: IndexPath) -> NSAttributedString? {
let name = message.sender.displayName
return NSAttributedString(string: name, attributes: [NSAttributedStringKey.font: UIFont.preferredFont(forTextStyle: .caption1)])
return NSAttributedString(string: name, attributes: [NSAttributedStringKey.font: UIFont.preferredFont(forTextStyle: .caption1)])
}
func cellBottomLabelAttributedText(for message: MessageType, at indexPath: IndexPath) -> NSAttributedString? {
let formatter = DateFormatter()
formatter.dateStyle = .medium
struct ConversationDateFormatter {
static let formatter: DateFormatter = {
let formatter = DateFormatter()
formatter.dateStyle = .medium
return formatter
}()
}
let formatter = ConversationDateFormatter.formatter
let dateString = formatter.string(from: message.sentDate)
return NSAttributedString(string: dateString, attributes: [NSAttributedStringKey.font: UIFont.preferredFont(forTextStyle: .caption2)])
return NSAttributedString(string: dateString, attributes: [NSAttributedStringKey.font: UIFont.preferredFont(forTextStyle: .caption2)])
}
}