🚨🚨 THIS LIBRARY IS NO LONGER BEING MAINTAINED 🚨🚨
I've made a career move away from iOS and decided to stop maintaining this library due to Swift's continued instability.
The amazing folks at Neone have forked the project and continued to develop it. Please use their fork located here: https://github.com/neone/NDAvatarApp.
Branch | Build Status |
---|---|
Develop | |
Master |
AvatarImageView
is a customisable subclass of UIImageView that is designed to show users' profile pictures. It falls back to the user's initials with a random background color if no profile picture is supplied.
This library was inspired by and is supposed to be a Swift rewrite of bachonk/UIImageView-Letters.
To set up AvatarImageView
, a dataSource
that conforms to AvatarImageViewDataSource
needs to be set. Optionally a configuration
that conforms to AvatarImageViewConfiguration
can also be set. The default configuration will show a square picture; and if no profile picture is supplied, it will draw the initials with the system font on a random background color.
The AvatarImageViewDataSource
contains the following members. All have default implementations and are hence optional.
var name: String { get }
- Default: returns""
var avatar: UIImage? { get }
- Default: returnsnil
var bgColor: UIColor? { get }
- Default: returnsnil
var initials: String { get }
- Default: returns initials calculated from the name.var avatarId: Int { get }
- Default: returns the hash values of the name and initials combined using XOR.
The AvatarImageViewConfiguration
contains the following members. All have default implementations and are hence optional.
var shape: Shape { get }
- Default: returns.Square
var textSizeFactor: CGFloat { get }
- Default: returns0.5
var fontName: String? { get }
- Default: returnsnil
var bgColor: UIColor? { get }
- Default: returns innil
. ThebgColor
inAvatarImageViewDataSource
will take precedence over this one.var textColor: UIColor { get }
- Default: returns.white
.
Check out the docs for more information.
The random background colour is generated for each unique user from its avatarId
, so if you have AvatarImageView
s in different parts of your app, the background color for a particular user will be the same in both.
The image view can be drawn as a square or circle out of the box. You can even sepcify a mask image if you want a custom shape. These settings are done in an AvatarImageViewConfiguration
. Here are some examples for initials being drawn in different shapes.
Here's an example of when the dataSource
supplies a profile picture and the configuration
is set to a circle.
It works great with custom fonts!
...and also with UITableView
s
Please refer to the example project in this repository for an implementation of all the above use cases. If you find any bugs, open a GitHub issue!
-
Always set the
configuration
before thedataSource
. If you don't, you will need to manually callrefresh()
to render the view correctly. -
When implementing the
AvatarImageViewDataSource
andAvatarImageViewConfiguration
protocols, you will need to explicitly define the type for any protocol member that is an optional otherwise Swift goes insane.
For example, AvatarImageViewConfiguration
has a type called var fontName: String?
that returns nil
by default. To implement this in a struct
, define it as follows:
struct Config: AvatarImageViewConfiguration {
var fontName: String? = "Futura-Medium"
}
Defining it as:
struct Config: AvatarImageViewConfiguration {
var fontName = "Futura-Medium"
}
... will not work :(
Docs are available on CocoaDocs
AvatarImageView requires at least iOS 8.
AvatarImageView is available through CocoaPods. To install it, add the following lines to your Podfile:
use_frameworks!
# Swift 2.2
pod "AvatarImageView", '1.1.1'
# Swift 2.3
pod "AvatarImageView", '~> 1.2.1'
# Swift 3.0
pod "AvatarImageView", '2.0.0'
# Swift 4.0
pod "AvatarImageView", '~> 2.1.0'
# Swift 4.2
pod "AvatarImageView", '~> 2.2.0'
Added Swift 4.2 support
Fixed irrational test suite and test warnings
Migrated to Swift 4
Added a baseline offset config attribute which may be needed for custom fonts
Fixing an issue where a profile picture may not always appear in a circle even if specified in the configuration.
Modified init(frame:)
to be public. Seems to have reversed from earlier by a bad merge.
Migrated code to Swift 3.0.
Set deployment target to 8.0. No further features or patches will be issued for Swift 2.3 after this release.
Migrated code to Swift 2.3.
Modified init(frame:)
to be public.
Improved the random color generator. It now generates a hash from the initials and name, and then uses that as the seed to generate the random color, so it's always the same for each unique user.
Initial Release
Ayush Newatia, [email protected]
AvatarImageView is available under the MIT license. See the LICENSE.md file for more info.