LetterAvatarKit
LetterAvatarKit
为生成基于字母的头像/占位符提供了 UIImage
扩展。有一些图片显示了使用此框架可以达到的效果
要求
- iOS 8+
- tvOS 9+
- Swift 4.0+
功能
- 易于使用
- 灵活的 API
- 构建模式
- 圆形/方形/带边框的图像
- 扁平UI颜色
- tvOS 支持
安装
CocoaPods
LetterAvatarKit
可以通过 CocoaPods 获取。要安装它,只需将以下行添加到您的 Podfile
pod "LetterAvatarKit", "1.2.5" # Swift 5.0+, Xcode 12
pod "LetterAvatarKit", "1.2.3" # Swift 5.0+, Xcode 11
pod "LetterAvatarKit", "1.1.7" # Swift 4.2
pod "LetterAvatarKit", "1.1.5" # Swift 4.0
Carthage
将其添加到 Cartfile
github "vpeschenkov/LetterAvatarKit" == 1.2.5 # Swift 5.0+, Xcode 12
github "vpeschenkov/LetterAvatarKit" == 1.2.3 # Swift 5.0+, Xcode 11
github "vpeschenkov/LetterAvatarKit" == 1.1.7 # Swift 4.2
github "vpeschenkov/LetterAvatarKit" == 1.1.5 # Swift 4.0
$ carthage update
使用说明
Swift
使用 LetterAvatarMaker
// Square avatar image
let avatarImage = LetterAvatarMaker()
.setUsername("Letter Avatar")
.build()
avatarImageView.image = avatarImage
// Circle avatar image with white border
let circleAvatarImage = LetterAvatarMaker()
.setCircle(true)
.setUsername("Letter Avatar")
.setBorderWidth(1.0)
.setBackgroundColors([ .red ])
.build()
avatarImageView.image = circleAvatarImage
使用 LetterAvatarMaker
与闭包结合
let avatarImage = LetterAvatarMaker()
.build { c in
c.username = "Letter Avatar"
}
avatarImageView.image = avatarImage
let avatarImage = LetterAvatarMaker()
.build { c in
c.circle = true
c.username = "Letter Avatar"
c.borderWidth = 1.0
c.backgroundColors = [ .red ]
}
avatarImageView.image = avatarImage
使用 LetterAvatarBuilderConfiguration
let configuration = LetterAvatarBuilderConfiguration()
configuration.username = "Letter Avatar"
avatarImageView.image = UIImage.makeLetterAvatar(withConfiguration: configuration)
使用 UIImage 扩展
avatarImageView.image = UIImage.makeLetterAvatar(withUsername: "Letter Avatar")
Objective-C
使用 LKLetterAvatarBuilderCongiguration
LKLetterAvatarBuilderCongiguration *configuration = [[LKLetterAvatarBuilderCongiguration alloc] init];
configuration.username = @"Letter Avatar";
self.avatarImageView.image = [UIImage lk_makeLetterAvatarWithConfiguration:configuration];
使用 UIImage 扩展
self.avatarImageView.image = [UIImage lk_makeLetterAvatarWithUsername:@"Letter Avatar"];
自定义
您可以配置以下 LetterAvatarBuilderConfiguration
的属性
/// The username.
open var username: String?
/// The size of an avatar image.
open var size: CGSize = CGSize(width: 80, height: 80)
/// The flag that indicates of using single only one letter, otherwise,
/// as much as wil be possible to obtain. But no more than 3 letters.
open var isSingleLettered: Bool = false
/// The letters font.
open var lettersFont: UIFont = UIFont.systemFont(ofSize: 16.0)
/// The letters colors
open var lettersColor: UIColor = LKUIColorByRGB(red: 236, green: 240, blue: 241)
/// The background colors of an image.
open var backgroundColors: [UIColor] = UIColor.colors
/// The letters font attributes.
open var lettersFontAttributes: [NSAttributedString.Key: Any]?
/// Indicates whether to generate circle or square image.
open var circle: Bool = false
/// The border width of the image.
open var borderWidth: CGFloat = 0.0
/// The border color of the image.
open var borderColor: UIColor = UIColor.white
/// A Boolean flag indicating whether the avatar is opaque.
open var opaque: Bool = false
社区
欢迎提问、评论、问题和Pull请求!
联系方式
许可
本项目采用MIT许可。更多信息请查看 LICENSE。