InitialImageView is a subclass of UIImageView that empower user to set initial or name as a placeholder image like what you see in a iOs Contact
Use CocoaPods with Podfile:
pod 'InitialImageView'
Just drag and drop InitialImageView.swift to your project.
@IBOutlet weak var initialImageView: InitialImageView!
override func viewDidLoad() {
super.viewDidLoad()
initialWithFontImageView.setImageWithInitial("YW", backgroundColor: UIColor.greenColor())
}
override func viewDidLoad() {
super.viewDidLoad()
let initialImageView = InitialImageView(frame: CGRect(x: 0, y: 0, width: 80, height: 80))
initialWithFontImageView.setImageWithInitial("YW", backgroundColor: UIColor.greenColor())
}
profileImageView.setImageWithFirstName(user.firstName, lastName: user.lastName)
profileImageView.af_setImageWithURL(user.profileUrl)
-
Create or change your UIImageView to InitialImageView
-
Call any of the following function to set image for InitialImageView
func setImageWithFirstName(firstName: String, lastName: String, backgroundColor: UIColor = UIColor.darkGrayColor(), randomColor: Bool = false) func setImageWithInitial(initial: String, backgroundColor: UIColor = UIColor.darkGrayColor(), randomColor: Bool = false) func setImageWithInitial(initial: String, backgroundColor: UIColor = UIColor.darkGrayColor(), randomColor: Bool = false)
backgroundColor
default is UIColor.darkGrayColor()
however it can be set by any UIColor that user has passed in when calling the function.
randomColor
default is false. By setting randomColor
to true, it will ignore the backgroundColor
parameters and generate a random UIColor that is not 'near' white.
- Properties:
The following properties could be set if needed to override the default value
var fontResizeValue: CGFloat
var font: UIFont
var isCircle: Bool
fontResizeValue
is used to calculate the font size with self.bounds
font
will be used to draw the initial UIImage that will be set to InitialImageView. Setting fontsize on 'font' here will not take any effect
isCircle
is a boolean that will determine if InitialImageView should be a circle
This project draw inspiration from UIImageView+Letters in Objective-c. I have used the Pods multiple time in Objective-c project. I decided to write it in Swift with a few changes and also turning it into a subclass instead.
InitialImageView is released under the MIT license. See LICENSE for details.