Create images with initials of names, for example Gmail in Swift, programmatically for iOS

I need to display a pic of the profile of each user corresponding to his name in a UITableView. Until the image is uploaded, I need to show the image with its name, the first alphabet, as in the GMail application. How to do this programmatically in Swift for iOS?

enter image description here enter image description here

+7
ios swift
source share
5 answers

Using the FredLoh Offer:

I made UILabel (nameInitialLabel) in the storyboard. Adjust the size and font.

func setDefaultImage(name: String) { nameInitialLabel.text = String(name[name.startIndex]) nameInitialLabel.backgroundColor = pickColor(name[name.startIndex]) nameInitialLabel.enabled = true } func pickColor(alphabet: Character) -> UIColor { let alphabetColors = [0x5A8770, 0xB2B7BB, 0x6FA9AB, 0xF5AF29, 0x0088B9, 0xF18636, 0xD93A37, 0xA6B12E, 0x5C9BBC, 0xF5888D, 0x9A89B5, 0x407887, 0x9A89B5, 0x5A8770, 0xD33F33, 0xA2B01F, 0xF0B126, 0x0087BF, 0xF18636, 0x0087BF, 0xB2B7BB, 0x72ACAE, 0x9C8AB4, 0x5A8770, 0xEEB424, 0x407887] let str = String(alphabet).unicodeScalars let unicode = Int(str[str.startIndex].value) if 65...90 ~= unicode { let hex = alphabetColors[unicode - 65] return UIColor(red: CGFloat(Double((hex >> 16) & 0xFF)) / 255.0, green: CGFloat(Double((hex >> 8) & 0xFF)) / 255.0, blue: CGFloat(Double((hex >> 0) & 0xFF)) / 255.0, alpha: 1.0) } return UIColor.blackColor() } 

I extracted the alphabetColors mapping array from https://github.com/uttesh/ngletteravatar

+3
source share

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

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

[EDIT]

You can also check this: https://github.com/bofiaza/IPImage

+11
source share

enter image description here

"NEED ANY FRAMEWORK"

"His work is beautiful."

 @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() } 
+9
source share

Add the code here . I suggest you add SnapKit

Add this code for you to generate cells:

 let profileView = UIView() cell.addSubview(profileView) profileView.snp_makeConstraints { (make) -> Void in make.left.equalTo(cell).offset(10) make.centerY.equalTo(cell) make.height.width.equalTo(30) //Your color profileView.backgroundColor = UIColor.greenColor() let firstLetter = UILabel() profileView.addSubview(firstLetter) firstLetter.text = yourString[0] //Add constraint for it, I suggest using SnapKit in which case firstLetter.snp_makeConstraints { (make) -> Void in make.center.equalTo(profileView) } 
+2
source share

I wrote the Swift library for this: https://github.com/ayushn21/AvatarImageView

It is highly customizable and uses a protocol-oriented approach to retrieve data and configuration

0
source share

All Articles