mirror of https://github.com/oxen-io/session-ios
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
74 lines
2.5 KiB
Swift
74 lines
2.5 KiB
Swift
5 years ago
|
import CryptoSwift
|
||
|
|
||
|
public class PlaceholderIcon {
|
||
|
private let seed: Int
|
||
|
// Colour palette
|
||
|
private var colours: [UIColor] = [
|
||
|
0x5ff8b0,
|
||
|
0x26cdb9,
|
||
|
0xf3c615,
|
||
|
0xfcac5a
|
||
5 years ago
|
].map { UIColor(hex: $0) }
|
||
5 years ago
|
|
||
|
init(seed: Int, colours: [UIColor]? = nil) {
|
||
|
self.seed = seed
|
||
5 years ago
|
if let colours = colours { self.colours = colours }
|
||
5 years ago
|
}
|
||
|
|
||
|
convenience init(seed: String, colours: [UIColor]? = nil) {
|
||
|
// Ensure we have a correct hash
|
||
|
var hash = seed
|
||
5 years ago
|
if (hash.matches("^[0-9A-Fa-f]+$") && hash.count >= 12) { hash = seed.sha512() }
|
||
5 years ago
|
|
||
|
guard let number = Int(hash.substring(to: 12), radix: 16) else {
|
||
5 years ago
|
owsFailDebug("Failed to generate number from seed string: \(seed).")
|
||
5 years ago
|
self.init(seed: 0, colours: colours)
|
||
|
return
|
||
|
}
|
||
|
|
||
|
self.init(seed: number, colours: colours)
|
||
|
}
|
||
|
|
||
5 years ago
|
public func generateLayer(with diameter: CGFloat, text: String) -> CALayer {
|
||
5 years ago
|
let colour = self.colours[seed % self.colours.count].cgColor
|
||
|
let base = getTextLayer(with: diameter, colour: colour, text: text)
|
||
|
base.masksToBounds = true
|
||
|
return base
|
||
|
}
|
||
|
|
||
|
private func getTextLayer(with diameter: CGFloat, colour: CGColor? = nil, text: String) -> CALayer {
|
||
5 years ago
|
let text = text.capitalized
|
||
5 years ago
|
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
|
||
|
let frame = CGRect(x: 0, y: (diameter - height) / 2, width: diameter, height: height)
|
||
5 years ago
|
|
||
|
let layer = CATextLayer()
|
||
|
layer.frame = frame
|
||
|
layer.foregroundColor = UIColor.white.cgColor
|
||
|
layer.contentsScale = UIScreen.main.scale
|
||
|
|
||
5 years ago
|
let fontName = font.fontName
|
||
|
let fontRef = CGFont(fontName as CFString)
|
||
|
layer.font = fontRef
|
||
|
layer.fontSize = font.pointSize
|
||
5 years ago
|
layer.alignmentMode = .center
|
||
|
|
||
|
layer.string = text
|
||
|
|
||
|
let base = CALayer()
|
||
|
base.frame = CGRect(x: 0, y: 0, width: diameter, height: diameter)
|
||
|
base.backgroundColor = colour
|
||
|
base.addSublayer(layer)
|
||
|
|
||
|
return base
|
||
|
}
|
||
|
}
|
||
5 years ago
|
|
||
|
private extension String {
|
||
|
|
||
|
func matches(_ regex: String) -> Bool {
|
||
|
return self.range(of: regex, options: .regularExpression, range: nil, locale: nil) != nil
|
||
|
}
|
||
|
}
|