Make images with name initials like Gmail in Swift programmatically for iOS

enter image description here

"NO NEED ANY FRAMEWORK"

"Its Work Wonderfully"

@IBOutlet weak var IBtxtFieldName:UITextField!
@IBOutlet weak var IBtxtFieldSurname:UITextField!
@IBOutlet weak var IBImgViewUserProfile:UIImageView!


@IBAction func IBbtnShowTapped(sender: UIButton)
{
    let lblNameInitialize = UILabel()
    lblNameInitialize.frame.size = CGSize(width: 100.0, height: 100.0)
    lblNameInitialize.textColor = UIColor.whiteColor()
    lblNameInitialize.text = String(IBtxtFieldName.text!.characters.first!) + String(IBtxtFieldSurname.text!.characters.first!)
    lblNameInitialize.textAlignment = NSTextAlignment.Center
    lblNameInitialize.backgroundColor = UIColor.blackColor()
    lblNameInitialize.layer.cornerRadius = 50.0

    UIGraphicsBeginImageContext(lblNameInitialize.frame.size)
    lblNameInitialize.layer.renderInContext(UIGraphicsGetCurrentContext()!)
    IBImgViewUserProfile.image = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()
}

"SWIFT 3.0"

@IBAction func IBbtnShowTapped(sender: UIButton)
{
    let lblNameInitialize = UILabel()
    lblNameInitialize.frame.size = CGSize(width: 100.0, height: 100.0)
    lblNameInitialize.textColor = UIColor.white
    lblNameInitialize.text = String(IBtxtFieldName.text!.characters.first!) + String(IBtxtFieldSurname.text!.characters.first!)
    lblNameInitialize.textAlignment = NSTextAlignment.center
    lblNameInitialize.backgroundColor = UIColor.black
    lblNameInitialize.layer.cornerRadius = 50.0

    UIGraphicsBeginImageContext(lblNameInitialize.frame.size)
    lblNameInitialize.layer.render(in: UIGraphicsGetCurrentContext()!)
    IBImgViewUserProfile.image = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()
}

This is perfect for the UIImageView: https://github.com/bachonk/UIImageView-Letters. Basically, it creates a UIImage with, at the center, the initial letter of the first and last word of the input. The background color can be random or assigned.

Here's an example of what this category can do: enter image description here

[EDIT]

You may also want to check this out: https://github.com/bofiaza/IPImage

Tags:

Ios

Swift