-
Notifications
You must be signed in to change notification settings - Fork 1
/
InitialImageView.swift
106 lines (81 loc) · 4.13 KB
/
InitialImageView.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
//
// InitialImageView.swift
// InitialImageViewExample
//
// Created by Zac on 21/5/16.
// Copyright © 2016 Zac. All rights reserved.
//
import UIKit
public class InitialImageView : UIImageView {
public var fontResizeValue:CGFloat = 0.5
public var font = UIFont.systemFontOfSize(20) //font size does not matter, it will be calculated by the base on fontResizeValue and self.bounds
public var isCircle = true
public func setImageWithFirstName(firstName: String, lastName: String, backgroundColor: UIColor = UIColor.darkGrayColor(), randomColor: Bool = false) {
self.setImageWithInitial(getInitialFromName("\(firstName) \(lastName)"), backgroundColor: backgroundColor, randomColor: randomColor)
}
public func setImageWithName(name: String, backgroundColor: UIColor = UIColor.darkGrayColor(), randomColor: Bool = false) {
self.setImageWithInitial(getInitialFromName(name), backgroundColor: backgroundColor, randomColor: randomColor)
}
public func setImageWithInitial(initial: String, backgroundColor: UIColor = UIColor.darkGrayColor(), randomColor: Bool = false) {
let attributedInitial = NSAttributedString(string: initial, attributes: createFontAttribute())
if randomColor {
let bgColor = UIColor.randomNonNearWhiteColor()
self.image = createImageFromInitial(attributedInitial, backgroundColor: bgColor)
if backgroundColor != UIColor.darkGrayColor() {
print("Warning: backgroundColor parameters will be ignored if randomColor is set to true")
}
} else {
self.image = createImageFromInitial(attributedInitial, backgroundColor: backgroundColor)
}
}
// MARK: - Helpers
func getInitialFromName(name: String) -> String {
let fullName = name.componentsSeparatedByString(" ")
var initial = String()
if let firstChar = fullName[safe: 0]?.characters.first {
initial.append(firstChar)
}
if let secondChar = fullName[safe: 1]?.characters.first {
initial.append(secondChar)
}
return initial
}
func createFontAttribute() -> [String: AnyObject] {
let fontSize = CGRectGetWidth(self.bounds) * fontResizeValue;
let fontAttribute:[String:AnyObject] = [NSForegroundColorAttributeName: UIColor.whiteColor(), NSFontAttributeName: font.fontWithSize(fontSize)]
return fontAttribute
}
private func createImageFromInitial(attributedInitial: NSAttributedString, backgroundColor: UIColor) -> UIImage {
let scale = UIScreen.mainScreen().scale
let bounds = self.bounds
UIGraphicsBeginImageContextWithOptions(bounds.size, false, scale)
let context = UIGraphicsGetCurrentContext()
if (isCircle) {
let path = CGPathCreateWithEllipseInRect(self.bounds, nil);
CGContextAddPath(context, path)
CGContextClip(context)
}
CGContextSetFillColorWithColor(context, backgroundColor.CGColor)
CGContextFillRect(context, CGRectMake(0, 0, bounds.size.width, bounds.size.height))
let textSize = attributedInitial.size()
let rect = CGRect(x: bounds.size.width/2 - textSize.width/2, y: bounds.size.height/2 - textSize.height/2, width: textSize.width, height: textSize.height)
attributedInitial.drawInRect(rect)
let image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
}
extension UIColor {
static func randomNonNearWhiteColor() -> UIColor {
//upper bounds is set to 215 to prevent color that is "near" white which result in no differences between initial and bg color
let red = CGFloat(arc4random_uniform(215)) / 255
let green = CGFloat(arc4random_uniform(215)) / 255
let blue = CGFloat(arc4random_uniform(215)) / 255
return UIColor(red: red, green: green, blue: blue, alpha: 1)
}
}
extension Array {
subscript (safe index: Int) -> Element? {
return indices ~= index ? self[index] : nil
}
}