Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

ios

swift

I need to display a profile pic of every user corresponding to his name in a UITableView. Until the image is downloaded I need to show an image with his name's first alphabet like in the GMail app. How to do this programmatically in Swift for iOS?

enter image description here enter image description here

like image 842
Lakhshya Bansal Avatar asked Dec 05 '15 07:12

Lakhshya Bansal


2 Answers

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()
}
like image 85
Er.Gopal Vasani Avatar answered Nov 15 '22 16:11

Er.Gopal Vasani


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

like image 31
yoninja Avatar answered Nov 15 '22 17:11

yoninja