Selaa lähdekoodia

intialsLabel designed and integrated into chatView navbar (rightBarButtonItem)

Bastian van de Wetering 6 vuotta sitten
vanhempi
commit
613daa9fac

+ 2 - 1
deltachat-ios/Controller/ChatViewController.swift

@@ -61,7 +61,8 @@ class ChatViewController: MessagesViewController {
 		if let image = chat.profileImage {
 			navigationItem.rightBarButtonItem = UIBarButtonItem(image: image, style: .done, target: self, action: #selector(chatProfilePressed))
 		} else {
-			navigationItem.rightBarButtonItem = UIBarButtonItem(title: chat.name, style: .done, target: self, action: #selector(chatProfilePressed))
+			let initialsLabel = InitialsLabel(name: chat.name, color: chat.color, size: 28)
+			navigationItem.rightBarButtonItem = UIBarButtonItem(customView: initialsLabel)
 		}
 
 

+ 94 - 79
deltachat-ios/View/ContactCell.swift

@@ -9,13 +9,44 @@
 import UIKit
 
 class ContactCell: UITableViewCell {
-  let avatar = UIView()
-  let imgView = UIImageView()
-  let initialsLabel = UILabel()
+	private let initialsLabelSize: CGFloat = 54
+	private let imgSize: CGFloat = 25
+
+
+	let avatar: UIView = {
+		let avatar = UIView()
+		return avatar
+	}()
+
+	lazy var imgView: UIImageView = {
+		let imgView = UIImageView()
+		let img = UIImage(named: "approval")!.withRenderingMode(.alwaysTemplate)
+		imgView.isHidden = true
+		imgView.image = img
+		imgView.bounds = CGRect(
+			x: 0,
+			y: 0,
+			width: imgSize, height: imgSize
+		)
+		return imgView
+	}()
+
+	lazy var initialsLabel: UILabel = {
+		let initialsLabel = UILabel()
+		initialsLabel.textAlignment = NSTextAlignment.center
+		initialsLabel.textColor = UIColor.white
+		initialsLabel.font = UIFont.systemFont(ofSize: 22)
+		initialsLabel.backgroundColor = UIColor.green
+		let initialsLabelCornerRadius = (initialsLabelSize - 6) / 2
+		initialsLabel.layer.cornerRadius = initialsLabelCornerRadius
+		initialsLabel.clipsToBounds = true
+		return initialsLabel
+	}()
+
   let nameLabel = UILabel()
-  let emailLabel = UILabel()
+	let emailLabel = UILabel()
 
-  var darkMode: Bool = false {
+	var darkMode: Bool = false {
     didSet {
       if darkMode {
         contentView.backgroundColor = UIColor.darkGray
@@ -27,79 +58,64 @@ class ContactCell: UITableViewCell {
 
   override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
     super.init(style: style, reuseIdentifier: reuseIdentifier)
+		setupSubviews()
+    }
 
-    // configure and layout initialsLabel
-    let initialsLabelSize: CGFloat = 54
-    let initialsLabelCornerRadius = (initialsLabelSize - 6) / 2
-    let margin: CGFloat = 15
-
-    initialsLabel.textAlignment = NSTextAlignment.center
-    initialsLabel.textColor = UIColor.white
-    initialsLabel.font = UIFont.systemFont(ofSize: 22)
-    initialsLabel.translatesAutoresizingMaskIntoConstraints = false
-    avatar.translatesAutoresizingMaskIntoConstraints = false
-    initialsLabel.widthAnchor.constraint(equalToConstant: initialsLabelSize - 6).isActive = true
-    initialsLabel.heightAnchor.constraint(equalToConstant: initialsLabelSize - 6).isActive = true
-    // avatar.backgroundColor = .red
-
-    avatar.widthAnchor.constraint(equalToConstant: initialsLabelSize).isActive = true
-    avatar.heightAnchor.constraint(equalToConstant: initialsLabelSize).isActive = true
-
-    initialsLabel.backgroundColor = UIColor.green
-
-    initialsLabel.layer.cornerRadius = initialsLabelCornerRadius
-    initialsLabel.clipsToBounds = true
-    avatar.addSubview(initialsLabel)
-    contentView.addSubview(avatar)
-
-    initialsLabel.topAnchor.constraint(equalTo: avatar.topAnchor, constant: 3).isActive = true
-    initialsLabel.leadingAnchor.constraint(equalTo: avatar.leadingAnchor, constant: 3).isActive = true
-    initialsLabel.trailingAnchor.constraint(equalTo: avatar.trailingAnchor, constant: -3).isActive = true
-
-    avatar.leadingAnchor.constraint(equalTo: contentView.leadingAnchor, constant: margin).isActive = true
-    avatar.center.y = contentView.center.y
-    avatar.center.x += initialsLabelSize / 2
-    avatar.topAnchor.constraint(equalTo: contentView.topAnchor, constant: margin).isActive = true
-    avatar.bottomAnchor.constraint(lessThanOrEqualTo: contentView.bottomAnchor, constant: -margin).isActive = true
-    initialsLabel.center = avatar.center
-
-    let myStackView = UIStackView()
-    myStackView.translatesAutoresizingMaskIntoConstraints = false
-    myStackView.clipsToBounds = true
-
-    contentView.addSubview(myStackView)
-    myStackView.leadingAnchor.constraint(equalTo: avatar.trailingAnchor, constant: margin).isActive = true
-    myStackView.centerYAnchor.constraint(equalTo: avatar.centerYAnchor).isActive = true
-    myStackView.trailingAnchor.constraint(equalTo: contentView.trailingAnchor, constant: -margin).isActive = true
-    myStackView.axis = .vertical
-    myStackView.addArrangedSubview(nameLabel)
-    myStackView.addArrangedSubview(emailLabel)
-
-    nameLabel.font = UIFont.systemFont(ofSize: 16, weight: .medium)
-    nameLabel.lineBreakMode = .byTruncatingTail
-    nameLabel.textColor = UIColor(hexString: "2f3944")
-
-    emailLabel.font = UIFont.systemFont(ofSize: 14)
-    emailLabel.textColor = UIColor(hexString: "848ba7")
-    emailLabel.lineBreakMode = .byTruncatingTail
-
-    let img = UIImage(named: "approval")!.withRenderingMode(.alwaysTemplate)
-    let imgSize: CGFloat = 25
-
-    imgView.isHidden = true
-    imgView.image = img
-    imgView.bounds = CGRect(
-      x: 0,
-      y: 0,
-      width: imgSize, height: imgSize
-    )
-    imgView.tintColor = DCColors.primary
-
-    avatar.addSubview(imgView)
-
-    imgView.center.x = avatar.center.x + (avatar.frame.width / 2) + imgSize - 5
-    imgView.center.y = avatar.center.y + (avatar.frame.height / 2) + imgSize - 5
-  }
+	private func setupSubviews() {
+		let margin: CGFloat = 15
+
+		initialsLabel.translatesAutoresizingMaskIntoConstraints = false
+		avatar.translatesAutoresizingMaskIntoConstraints = false
+		initialsLabel.widthAnchor.constraint(equalToConstant: initialsLabelSize - 6).isActive = true
+		initialsLabel.heightAnchor.constraint(equalToConstant: initialsLabelSize - 6).isActive = true
+		// avatar.backgroundColor = .red
+
+		avatar.widthAnchor.constraint(equalToConstant: initialsLabelSize).isActive = true
+		avatar.heightAnchor.constraint(equalToConstant: initialsLabelSize).isActive = true
+
+		avatar.addSubview(initialsLabel)
+		contentView.addSubview(avatar)
+
+		initialsLabel.topAnchor.constraint(equalTo: avatar.topAnchor, constant: 3).isActive = true
+		initialsLabel.leadingAnchor.constraint(equalTo: avatar.leadingAnchor, constant: 3).isActive = true
+		initialsLabel.trailingAnchor.constraint(equalTo: avatar.trailingAnchor, constant: -3).isActive = true
+
+		avatar.leadingAnchor.constraint(equalTo: contentView.leadingAnchor, constant: margin).isActive = true
+		avatar.center.y = contentView.center.y
+		avatar.center.x += initialsLabelSize / 2
+		avatar.topAnchor.constraint(equalTo: contentView.topAnchor, constant: margin).isActive = true
+		avatar.bottomAnchor.constraint(lessThanOrEqualTo: contentView.bottomAnchor, constant: -margin).isActive = true
+		initialsLabel.center = avatar.center
+
+		let myStackView = UIStackView()
+		myStackView.translatesAutoresizingMaskIntoConstraints = false
+		myStackView.clipsToBounds = true
+
+		contentView.addSubview(myStackView)
+		myStackView.leadingAnchor.constraint(equalTo: avatar.trailingAnchor, constant: margin).isActive = true
+		myStackView.centerYAnchor.constraint(equalTo: avatar.centerYAnchor).isActive = true
+		myStackView.trailingAnchor.constraint(equalTo: contentView.trailingAnchor, constant: -margin).isActive = true
+		myStackView.axis = .vertical
+		myStackView.addArrangedSubview(nameLabel)
+		myStackView.addArrangedSubview(emailLabel)
+
+		nameLabel.font = UIFont.systemFont(ofSize: 16, weight: .medium)
+		nameLabel.lineBreakMode = .byTruncatingTail
+		nameLabel.textColor = UIColor(hexString: "2f3944")
+
+		emailLabel.font = UIFont.systemFont(ofSize: 14)
+		emailLabel.textColor = UIColor(hexString: "848ba7")
+		emailLabel.lineBreakMode = .byTruncatingTail
+
+
+		imgView.tintColor = DCColors.primary
+
+		avatar.addSubview(imgView)
+
+		imgView.center.x = avatar.center.x + (avatar.frame.width / 2) + imgSize - 5
+		imgView.center.y = avatar.center.y + (avatar.frame.height / 2) + imgSize - 5
+
+	}
 
   func setVerified(isVerified: Bool) {
     imgView.isHidden = !isVerified
@@ -108,7 +124,6 @@ class ContactCell: UITableViewCell {
   func setImage(_ img: UIImage) {
     let attachment = NSTextAttachment()
     attachment.image = img
-
     initialsLabel.attributedText = NSAttributedString(attachment: attachment)
   }
 
@@ -121,7 +136,7 @@ class ContactCell: UITableViewCell {
     setColor(color)
   }
 
-  func setColor(_ color: UIColor) {
+	func setColor(_ color: UIColor) {
     initialsLabel.backgroundColor = color
   }
 

+ 23 - 2
deltachat-ios/View/InitialsLabel.swift

@@ -10,10 +10,31 @@ import UIKit
 
 class InitialsLabel: UILabel {
 
-	init(name: String, size: CGFloat, color: UIColor) {
-		self.text = name.i
+	convenience init(name: String, color: UIColor, size: CGFloat) {
+		self.init(size: size)
+		set(name: name)
+		set(color: color)
 	}
 
+	init(size: CGFloat) {
+		super.init(frame: CGRect(x: 0, y: 0, width: size, height: size))
+		textAlignment = NSTextAlignment.center
+		textColor = UIColor.white
+		adjustsFontSizeToFitWidth = true
+		let initialsLabelCornerRadius = size / 2
+		layer.cornerRadius = initialsLabelCornerRadius
+		clipsToBounds = true
+	}
 
+	required init?(coder aDecoder: NSCoder) {
+		fatalError("init(coder:) has not been implemented")
+	}
 
+	func set(name: String) {
+		self.text = Utils.getInitials(inputName: name)
+	}
+
+	func set(color: UIColor) {
+		backgroundColor = color
+	}
 }