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

后端 未结 8 2224
孤城傲影
孤城傲影 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:30

    Simple little extension, can be customised as needed.

    public extension UIImageView {
    
        func addInitials(first: String, second: String) {
            let initials = UILabel(frame: CGRect(x: 0, y: 0, width: self.bounds.width, height: self.bounds.height))
            initials.center = CGPoint(x: self.bounds.width / 2, y: self.bounds.height / 2)
            initials.textAlignment = .center
            initials.text = first + " " + second
            initials.textColor = .black
            self.addSubview(initials)
        }
    }
    

提交回复
热议问题