Browse Source

fix avatar images in chat titles (#179)

cyberta 5 years ago
parent
commit
eaa733b949

+ 4 - 3
deltachat-ios/Controller/ChatViewController.swift

@@ -89,12 +89,13 @@ class ChatViewController: MessagesViewController {
         let chat = DcChat(id: chatId)
         titleView.updateTitleView(title: chat.name, subtitle: chat.subtitle)
 
+        let badge: InitialsBadge
         if let image = chat.profileImage {
-            navigationItem.rightBarButtonItem = UIBarButtonItem(image: image, style: .done, target: self, action: #selector(chatProfilePressed))
+            badge =  InitialsBadge(image: image, size: 28)
         } else {
-            let initialsLabel =  InitialsBadge(name: chat.name, color: chat.color, size: 28)
-            navigationItem.rightBarButtonItem = UIBarButtonItem(customView: initialsLabel)
+            badge =  InitialsBadge(name: chat.name, color: chat.color, size: 28)
         }
+        navigationItem.rightBarButtonItem = UIBarButtonItem(customView: badge)
 
         configureMessageMenu()
 

+ 11 - 0
deltachat-ios/View/InitialsBadge.swift

@@ -16,6 +16,11 @@ class InitialsBadge: UIView {
         setColor(color)
     }
 
+    convenience init (image: UIImage, size: CGFloat) {
+        self.init(size: size)
+        setImage(image)
+    }
+
     init(size: CGFloat) {
         super.init(frame: CGRect(x: 0, y: 0, width: size, height: size))
         let initialsLabelCornerRadius = size / 2
@@ -43,6 +48,12 @@ class InitialsBadge: UIView {
         label.text = Utils.getInitials(inputName: name)
     }
 
+    func setImage(_ image: UIImage) {
+        let attachment = NSTextAttachment()
+        attachment.image = image
+        label.attributedText = NSAttributedString(attachment: attachment)
+    }
+
     func setColor(_ color: UIColor) {
         backgroundColor = color
     }