📦 An extension that generates letter-based avatars/placeholders

Overview

LetterAvatarKit

LetterAvatarKit provides an UIImage extension for generating letter-based avatars/placeholders. There are a few images showing what you can achive by using this framework:

Requirements

  • iOS 8+
  • tvOS 9+
  • Swift 4.0+

Features

  • Ease of use
  • Flexible API
  • Builder pattern
  • Circle/Square/Bordered image
  • Flat UI colors
  • tvOS support

Installation

CocoaPods

LetterAvatarKit is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod "LetterAvatarKit", "1.2.4" # 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

Add this to Cartfile

github "vpeschenkov/LetterAvatarKit" == 1.2.4 # 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

Usage

Swift

Using 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

Using LetterAvatarMaker with closures:

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

Using LetterAvatarBuilderConfiguration:

let configuration = LetterAvatarBuilderConfiguration()
configuration.username = "Letter Avatar"
avatarImageView.image = UIImage.makeLetterAvatar(withConfiguration: configuration)

Using UIImage extension:

avatarImageView.image = UIImage.makeLetterAvatar(withUsername: "Letter Avatar")

Objective-C

Using LKLetterAvatarBuilderCongiguration:

LKLetterAvatarBuilderCongiguration *configuration = [[LKLetterAvatarBuilderCongiguration alloc] init];
configuration.username = @"Letter Avatar";
self.avatarImageView.image = [UIImage lk_makeLetterAvatarWithConfiguration:configuration];

Using UIImage extension:

self.avatarImageView.image = [UIImage lk_makeLetterAvatarWithUsername:@"Letter Avatar"];

Customization

You can configure the following properties of 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

Community

Questions, comments, issues, and pull requests are welcome!

Contacts

License

Distributed under the MIT license. See LICENSE for more information.

Comments
  • Crash: Fatal error: cannot increment beyond endIndex

    Crash: Fatal error: cannot increment beyond endIndex

    https://github.com/vpeschenkov/LetterAvatarKit/blob/0ce7d0c41420516775a07e354a1ac9512208fd35/LetterAvatarKit/LetterAvatarBuilder.swift#L105

    If single Letter is passed as false but the string is a single char, this line fails due to out of bounds exception.

    I think it would be safer to avoid this crash inside the framework by checking the size of the string before subscripting.

    If the string is only 1 letter lenght, then the singleLetter configuration could be ignored

    bug 
    opened by racer1988 3
  • Draw circle image

    Draw circle image

    Hi,

    First of all, thank you very much for this library. Great job :) I was wondering how I can draw a circular image instead of a rectangular one?

    Nawras

    question 
    opened by nawrasg 2
  • Compilation issue Xcode 8 - swift 3.0

    Compilation issue Xcode 8 - swift 3.0

    HI ,

    I am getting compilation issue on this line.

    if let letter = firstComponent.first  
    

    the issue says "'String' has no member 'first'"

    can you add this extension to make it work ? on swift 3.0 as well.

    extension String{
        
        
        func first()->String{
            
            
            return String(self.characters.prefix(1))
            
        }    
    }
    
    
    enhancement question 
    opened by ijameelkhan 2
  • add support for tvos

    add support for tvos

    Summary

    Add support for tvOS

    Requirements (place an x in each [ ])

    • [ x] I've read and agree to the Code of Conduct.
    • [ ] I've written tests to cover the new code and functionality included in this PR.

    Not apply above

    opened by matteocrippa 1
  • Can I add Transparent Background Color to View

    Can I add Transparent Background Color to View

    Description

    Describe your issue here.

    Requirements ( Add Transparent Color to BackgroundColor Array )

    • When I set the block Color with alpha (0,0,0,0.12) but it is showing black color instead of transparent color

    Hexcode -- 1F000000 is Used

    Bug Report

    Filling out the following details about bugs will help us solve your issue sooner.

    Reproducible in:

    LetterAvatarKit version:

    iOS version: 13.3.1

    enhancement help wanted 
    opened by NatashaChhibber707 1
  • Single letter

    Single letter

    First of all: Thanks for the convenient library!

    I ran into an issue where my app crashed with a username that consists out of one letter only. Looking into your code, I saw that this is probably due to the fact that the single parameter in LetterAvatarBuilderConfiguration is always false. I changed it into:

    open var singleLetter: Bool {
            if let name = username, name.count == 1  {
                return true
            } else {
                return false
            }
        }
    

    in my local repo, but I can't push them to your repo to create a pull request. Would you mind giving me permission or changing this yourself?

    bug 
    opened by joris1995 1
  • Initials Generation

    Initials Generation

    Currently if the name you pass to use for the initials has more than one space, there will be more than two characters in the abbreviated initials view. Can you modify the code to look for both the first and last word and then capture the first letters of those instead?

    enhancement 
    opened by ghost 1
Releases(1.2.5)
Owner
Victor Peschenkov
An Objc/Swift Developer
Victor Peschenkov
Lightweight and customisable async image loading in SwiftUI. Supports on-disk storage, placeholders and more!

Asyncrounously download and display images in Swift UI. Supports progress indicators, placeholders and image transitions. RemoteImageView Asyncrounous

Callum Trounce 192 Dec 7, 2022
Generates an image that looks like LEGO Art.

LegoArtFilter Generates an image that looks like LEGO Art. This library supports both iOS (14≤) and macOS (11≤). Usage // Get CGImage from CIImage let

Takuto NAKAMURA (Kyome) 10 Jul 7, 2022
App that generates random images and attempts to classify them with MobileNetV2

Image-Classifier-App App that generates random images and attempts to classify t

Eli Hartnett 0 Jul 9, 2022
Siri Shortcuts extension for calculating NN-based image hash.

NNHash Siri Shortcuts extension for calculating NN-based image hash. Based on nhcalc.

Yi Xie 3 Aug 9, 2021
An image download extension of the image view written in Swift for iOS, tvOS and macOS.

Moa, an image downloader written in Swift for iOS, tvOS and macOS Moa is an image download library written in Swift. It allows to download and show an

Evgenii Neumerzhitckii 330 Sep 9, 2022
SwiftGif - A small UIImage extension with gif support.

SwiftGif - A small UIImage extension with gif support.

SwiftGif 1.3k Dec 20, 2022
SwiftColorArt is a demo application that includes Swift files with all classes and extension necessary to create a font color schema matching to an image

SwiftColorArt SwiftColorArt is a demo application that includes Swift files with all classes and extension necessary to create a font color schema mat

Jan Gregor Triebel 264 Jan 4, 2023
A UIImageView extension to let the picture-cutting with faces showing better

UIImageView-BetterFace A UIImageView extension to let the picture-cutting with faces showing better Last update in v0.2_stable : add a UIImage+BetterF

Croath Liu 779 Sep 1, 2022
An extension that gives UIImageView the ability to focus on faces within an image.

FaceAware Sometimes the aspect ratios of images we need to work with don't quite fit within the confines of our UIImageViews. In most cases we can use

Beau Nouvelle 3k Jan 3, 2023
A simple UIImageView extension for using initials as a profile image, written in swift

InitialsImageView An easy, helpful UIImageView extension that generates letter initials as a placeholder for user profile images, with a randomized ba

Tom Bachant 215 Dec 17, 2022
Async image downloader with Mem&Disk cached as a UIImageView extension

HBWebImage Example To run the example project, clone the repo, and run pod install from the Example directory first. Requirements Installation HBWebIm

haoboxuxu 1 Oct 22, 2022
An NSFW image detector for Swift built as an extension on UIImage.

Swift_NSFW_Detector An NSFW image detector for Swift built as an extension on UIImage. If you've ever allowed users to share images you are probably w

Chris Brown 5 Nov 27, 2022
An extension to SnapshotTesting which allows you to create HEIC images

?? SnapshotTestingHEIC An extension to SnapshotTesting which allows you to create HEIC images. The benefit of using HEIC instead of PNG is that it can

Aleksei Kakoulin 5 Dec 4, 2022
STDevRxExt contains some extension functions for RxSwift and RxCocoa which makes our live easy.

STDevRxExt Example To run the Example.playground, clone the repo, and run pod install from the Example directory first. Requirements iOS 9.0+ tvOS 9.0

STDev 6 Mar 26, 2021
CTPanoramaView is a library that displays spherical or cylindrical panoramas with touch or motion based controls.

CTPanoramaView is a high-performance library that uses SceneKit to display complete spherical or cylindrical panoramas with touch or motion based controls.

Salih Cihan Tek 1k Jan 2, 2023
Gifu adds protocol-based, performance-aware animated GIF support to UIKit.

Gifu adds protocol-based, performance-aware animated GIF support to UIKit. (It's also a prefecture in Japan). Install Swift Package Manager Add the fo

Reda Lemeden 2.8k Jan 7, 2023
A machine learning based emoji image classifier

BQBClassifier ??️ Download From App Store Given that my photo albums are mixed with various emojis that often spoil my good mood, I wrote such an app

Lakr Aream 38 Aug 30, 2022
XAnimatedImage is a performant animated GIF engine for iOS written in Swift based on FLAnimatedImage

XAnimatedImage is a performant animated GIF engine for iOS written in Swift based on FLAnimatedImage. An illustration is shown below: Features Plays m

Khaled Taha 561 Sep 9, 2022
An open source iOS framework for GPU-based image and video processing

GPUImage Brad Larson http://www.sunsetlakesoftware.com @bradlarson [email protected] Overview The GPUImage framework is a BSD-licensed iO

Brad Larson 20k Jan 1, 2023