InitialsImageView
这是一个简单且有用的UIImageView扩展,它使用随机背景色生成字母首字母作为用户配图图像的占位符
安装
CocoaPods
将此spec添加到您的podfile中
pod "InitialsImageView"
查看官方指南以开始使用CocoaPods。
手动操作
- 将
InitialsImageView.swift
文件拖到您的项目中 - 享受吧!
使用
方法
在任意 UIImageView
实例上调用以下方法以设置图片
setImageForName(string: String, backgroundColor: UIColor?, circular: Bool, textAttributes: [String: AnyObject]?)
string
是用于生成首字母的字符串。如果可能,这应该是用户的全名。
backgroundColor
是一个可选参数,用于设置图片的背景颜色。传入 nil
以自动为你生成颜色。
circular
是一个布尔参数,如果启用,会自动将图片裁剪成圆形。
textAttributes
是一个可选的预定义文本属性字典。你可以在 NSAttributedString 中找到可用的键列表。
示例
let randomImage: UIImageView = UIImageView.init(frame: CGRect(x: self.view.bounds.midX - 40, y: self.view.bounds.midY - 80 - 40, width: 80, height: 80))
randomImage.setImageForName(string: "Michael Bluth", backgroundColor: nil, circular: true, textAttributes: nil)
感谢
如果您喜欢这个工具,请通过下载受其启发的免费 Turnout 应用支持它!
许可
使用 MIT 许可。有关详细信息,请参阅许可文件。