Skip to content

Commit

Permalink
correct incoming message text color
Browse files Browse the repository at this point in the history
  • Loading branch information
or-else committed May 17, 2019
1 parent 33602b6 commit 4bbf1bb
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 10 deletions.
14 changes: 7 additions & 7 deletions Tinodios/AttribFormatter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,10 @@ class AttribFormatter: DraftyFormatter {

typealias CharacterStyle = [NSAttributedString.Key: Any]

let clicker: UITextViewDelegate?
let baseFont: UIFont?

init(baseFont font: UIFont?, clicker: UITextViewDelegate?) {
init(baseFont font: UIFont?) {
self.baseFont = font
self.clicker = clicker
}

// Inline image
Expand Down Expand Up @@ -160,19 +158,21 @@ class AttribFormatter: DraftyFormatter {
/// - Parameters:
/// - content: Drafty object to convert
/// - baseFont: base font to derive styles from.
/// - clicker: methods to call in response to touch events in formatted text.
/// - textColor: default text color.
/// - maxSize: maximum size of attached images
public static func toAttributed(_ content: Drafty, baseFont font: UIFont?, clicker: UITextViewDelegate?, maxSize: CGSize) -> NSAttributedString {
public static func toAttributed(_ content: Drafty, baseFont font: UIFont?, textColor color: UIColor?, maxSize: CGSize) -> NSAttributedString {

if content.isPlain {
let attributed = NSMutableAttributedString(string: content.string)
attributed.addAttributes([.font : (font ?? Constants.kDefaultFont)], range: NSRange(location: 0, length: attributed.length))
return attributed
}

let formatTree = content.format(formatter: AttribFormatter(baseFont: font, clicker: clicker))
let formatTree = content.format(formatter: AttribFormatter(baseFont: font))
let attributed = formatTree.toAttributed(baseFont: font ?? Constants.kDefaultFont, fontTraits: nil, size: maxSize)

if let color = color {
attributed.addAttribute(NSAttributedString.Key.foregroundColor, value: color, range: NSRange(location: 0, length: attributed.length))
}
return attributed
}

Expand Down
5 changes: 2 additions & 3 deletions Tinodios/MessageViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -346,8 +346,7 @@ extension MessageViewController: UICollectionViewDataSource {
if drafty.isPlain {
cell.content.text = drafty.string
} else {
let attributed = AttribFormatter.toAttributed(drafty, baseFont: Constants.kContentFont, clicker: nil, maxSize: maxSize)
cell.content.attributedText = attributed
cell.content.attributedText = AttribFormatter.toAttributed(drafty, baseFont: Constants.kContentFont, textColor: cell.content.textColor, maxSize: maxSize)
}
}

Expand Down Expand Up @@ -571,7 +570,7 @@ extension MessageViewController : UICollectionViewDelegateFlowLayout {
let attributedText: NSAttributedString

if let drafty = message.content {
attributedText = AttribFormatter.toAttributed(drafty, baseFont: Constants.kContentFont, clicker: nil, maxSize: CGSize(width: maxWidth, height: collectionView.frame.height * 0.66))
attributedText = AttribFormatter.toAttributed(drafty, baseFont: Constants.kContentFont, textColor: nil, maxSize: CGSize(width: maxWidth, height: collectionView.frame.height * 0.66))
} else {
attributedText = NSAttributedString(string: "none", attributes: [.font: Constants.kContentFont])
}
Expand Down
Binary file modified ios-chat.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 4bbf1bb

Please sign in to comment.