ソースを参照

limit letters in badges to maximum of 2 to avoid messi badges with dot-dot-dot

Bastian van de Wetering 6 年 前
コミット
5f256ee942
2 ファイル変更48 行追加31 行削除
  1. 5 1
      deltachat-ios/Helper/Utils.swift
  2. 43 30
      deltachat-ios/View/InitialsBadge.swift

+ 5 - 1
deltachat-ios/Helper/Utils.swift

@@ -17,7 +17,11 @@ struct Utils {
   }
 
   static func getInitials(inputName: String) -> String {
-    let nameParts = inputName.split(separator: " ")
+    var nameParts = inputName.split(separator: " ")
+		// this limits initials to max 2, otherwise just takes first letter to avoid messy badges
+		if nameParts.count > 2 {
+			nameParts = [nameParts[0]]
+		}
     let initials: [Character] = nameParts.compactMap { part in part.capitalized.first }
     let initialsString: String = String(initials)
     return initialsString

+ 43 - 30
deltachat-ios/View/InitialsBadge.swift

@@ -6,39 +6,52 @@
 //  Copyright © 2019 Jonas Reinsch. All rights reserved.
 //
 
-
 import UIKit
 
-class InitialsBadge: UILabel {
-  convenience init(name: String, color: UIColor, size: CGFloat) {
-    self.init(size: size)
-    setName(name)
-    setColor(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
+class InitialsBadge: UIView {
+
+	private var label: UILabel = {
+		let label = UILabel()
+		label.adjustsFontSizeToFitWidth = true
+		label.textAlignment = NSTextAlignment.center
+		label.textColor = UIColor.white
+		return label
+	}()
+
+	convenience init(name: String, color: UIColor, size: CGFloat) {
+		self.init(size: size)
+		setName(name)
+		setColor(color)
+	}
+
+	init(size: CGFloat) {
+		super.init(frame: CGRect(x: 0, y: 0, width: size, height: size))
+		let initialsLabelCornerRadius = size / 2
+		layer.cornerRadius = initialsLabelCornerRadius
 		translatesAutoresizingMaskIntoConstraints = false
 		heightAnchor.constraint(equalToConstant: size).isActive = true
 		widthAnchor.constraint(equalToConstant: size).isActive = true
-    clipsToBounds = true
-  }
-
-  required init?(coder _: NSCoder) {
-    fatalError("init(coder:) has not been implemented")
-  }
-
-  func setName(_ name: String) {
-    text = Utils.getInitials(inputName: name)
-  }
-
-  func setColor(_ color: UIColor) {
-    backgroundColor = color
-  }
+		clipsToBounds = true
+		setupSubviews()
+	}
+
+	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
+	}
+
+	required init?(coder _: NSCoder) {
+		fatalError("init(coder:) has not been implemented")
+	}
+
+	func setName(_ name: String) {
+		label.text = Utils.getInitials(inputName: name)
+	}
+
+	func setColor(_ color: UIColor) {
+		backgroundColor = color
+	}
 }
-