ios - 以编程方式在 Swift 中为 iOS 制作带有名称首字母的图像,例如 Gmail

标签 ios swift

我需要在 UITableView 中显示每个用户的个人资料图片,对应于他的名字。在下载图像之前,我需要像在 GMail 应用程序中一样显示带有他名字的第一个字母的图像。如何在 Swift for iOS 中以编程方式执行此操作?

enter image description here enter image description here

最佳答案

enter image description here

“不需要任何框架”

“它的工作很棒”

@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()
}

关于ios - 以编程方式在 Swift 中为 iOS 制作带有名称首字母的图像,例如 Gmail,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34102465/

相关文章:

ios - sqlite3 : Non-aligned pointer being freed on iOS

ios - UITableView + UITextField + UIDatePicker

ios - tableview 单元格内的类似 Firebase 的系统

ios - 使用委托(delegate)和协议(protocol)的 Collection View Cell Action

ios - 如何防止用户写除英文以外的任何内容 Ios

iOS 空 Firebase 推送通知不显示

iphone - 如何使用谓词获取 Core Data NSSet 关系的有序列表?

iOS:使用 Swift 使应用程序无响应

swift - 仅在 rx_tap 事件上调用方法

ios - 带有 Alamofire 4 正文数据的 POST 请求