Merge pull request #578 from mpretty-cyro/feature/two-character-initials

Update the fallback avatar to be two characters
This commit is contained in:
RyanZhao 2022-03-10 17:04:20 +11:00 committed by GitHub
commit a31cfd2142
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 11 additions and 2 deletions

View File

@ -8,7 +8,17 @@ public final class Identicon : NSObject {
if content.count > 2 && content.hasPrefix("05") {
content.removeFirst(2)
}
let layer = icon.generateLayer(with: size, text: content.substring(to: 1))
let initials: String = content
.split(separator: " ")
.compactMap { word in word.first.map { String($0) } }
.joined()
let layer = icon.generateLayer(
with: size,
text: (initials.count >= 2 ?
initials.substring(to: 2).uppercased() :
content.substring(to: 2).uppercased()
)
)
let rect = CGRect(origin: CGPoint.zero, size: layer.frame.size)
let renderer = UIGraphicsImageRenderer(size: rect.size)
return renderer.image { layer.render(in: $0.cgContext) }

View File

@ -37,7 +37,6 @@ public class PlaceholderIcon {
}
private func getTextLayer(with diameter: CGFloat, colour: CGColor? = nil, text: String) -> CALayer {
let text = text.capitalized
let font = UIFont.boldSystemFont(ofSize: diameter / 2)
let height = NSString(string: text).boundingRect(with: CGSize(width: diameter, height: CGFloat.greatestFiniteMagnitude),
options: .usesLineFragmentOrigin, attributes: [ NSAttributedString.Key.font : font ], context: nil).height