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

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

    Add the code from here. I suggest you also add SnapKit

    Add this code to however you are generating 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)
    }
    

提交回复
热议问题