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

后端 未结 8 2206
孤城傲影
孤城傲影 2021-02-05 18:03

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 l

相关标签:
8条回答
  • 2021-02-05 18:51

    "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()
    }
    
    0 讨论(0)
  • 2021-02-05 18:52

    I've written a Swift library for this : https://github.com/ayushn21/AvatarImageView

    It's highly customisable and uses a protocol oriented approach to get it's data and configuration

    0 讨论(0)
提交回复
热议问题