Sfoglia il codice sorgente

Merge pull request #228 from deltachat/fix_avatar_image_layout

Fix avatar image layout
björn petersen 5 anni fa
parent
commit
34f04b5476

+ 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()
 

+ 2 - 2
deltachat-ios/Helper/UIImage+Extension.swift

@@ -24,7 +24,7 @@ extension UIImage {
         self.init(cgImage: cgImage)
     }
 
-    func resizeImage(targetSize: CGSize) -> UIImage {
+    func resizeImage(targetSize: CGSize) -> UIImage? {
         let size = self.size
 
         let widthRatio  = targetSize.width  / size.width
@@ -44,6 +44,6 @@ extension UIImage {
         let newImage = UIGraphicsGetImageFromCurrentImageContext()
         UIGraphicsEndImageContext()
 
-        return newImage!
+        return newImage
     }
 }

+ 5 - 3
deltachat-ios/View/ContactCell.swift

@@ -165,9 +165,11 @@ class ContactCell: UITableViewCell {
     }
 
     func setImage(_ img: UIImage) {
-        let attachment = NSTextAttachment()
-        attachment.image = img
-        initialsLabel.attributedText = NSAttributedString(attachment: attachment)
+        if let resizedImg = img.resizeImage(targetSize: CGSize(width: initialsLabelSize - 6, height: initialsLabelSize - 6)) {
+            let attachment = NSTextAttachment()
+            attachment.image = resizedImg
+            initialsLabel.attributedText = NSAttributedString(attachment: attachment)
+        }
     }
 
     func setBackupImage(name: String, color: UIColor) {

+ 16 - 3
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
@@ -30,9 +35,9 @@ class InitialsBadge: UIView {
     private func setupSubviews() {
         addSubview(label)
         label.translatesAutoresizingMaskIntoConstraints = false
-        label.leadingAnchor.constraint(equalTo: leadingAnchor, constant: 2).isActive = true
-        label.trailingAnchor.constraint(equalTo: trailingAnchor, constant: -2).isActive = true
-        label.centerYAnchor.constraint(equalTo: centerYAnchor, constant: 0).isActive = true
+        label.leadingAnchor.constraint(equalTo: leadingAnchor).isActive = true
+        label.trailingAnchor.constraint(equalTo: trailingAnchor).isActive = true
+        label.centerYAnchor.constraint(equalTo: centerYAnchor).isActive = true
     }
 
     required init?(coder _: NSCoder) {
@@ -43,6 +48,14 @@ class InitialsBadge: UIView {
         label.text = Utils.getInitials(inputName: name)
     }
 
+    func setImage(_ image: UIImage) {
+        if let resizedImg = image.resizeImage(targetSize: CGSize(width: self.frame.width, height: self.frame.height)) {
+            let attachment = NSTextAttachment()
+            attachment.image = resizedImg
+            label.attributedText = NSAttributedString(attachment: attachment)
+        }
+    }
+
     func setColor(_ color: UIColor) {
         backgroundColor = color
     }