AGCInitials is an Objective-C category on UIImageView to set an image placeholder with the contact's initials (letters) and an automatically generated background color.
- Use it with one line of code
- The generated background color of the UIImageView will be the same for the same initials, so you can use it in your table/collection view. 😎
- You can set your own color palette for background colors
- Good test coverage 💪
Import the category
#import "UIImageView+AGCInitials.h"
and then call the method with the contact's name:
[self.imageView agc_setImageWithInitialsFromName:@"Mick Jagger"];
The image will then contain a placeholder with the initials "MJ" and a generated background color:
- You can set the initials directly, without specifying a name, please note that in this case the background color of the image will be the same for the same initials:
- (void)agc_setImageWithInitials:(nonnull NSString*)initials;
- The
initialsFromName
method for @"Mick Jagger" and @"Michael Jackson" returns MJ as initials, but the background color will be probably different, since it's generated from the name:
- By default the initials are distinguished by
@" "
, but you can change the separator using:
- (void)agc_setImageWithInitialsFromName:(nonnull NSString*)name separatedByString:(nonnull NSString*)separator;
- You can customize the font by specifying the text attributes:
NSDictionary* initialsTextAttributes = @{ NSFontAttributeName : [UIFont systemFontOfSize:20], NSForegroundColorAttributeName : [UIColor purpleColor] };
[cellImageView agc_setImageWithInitialsFromName:@"Mick Jagger" separatedByString:@" " withTextAttributes:initialsTextAttributes];
- You can also change the color palette used to generate background colors:
#import "AGCInitialsColors.h"
And then just set your palette before using the category:
NSArray<UIColor*>* yourColorPalette; //...
[[AGCInitialsColors sharedInstance] setColorPalette:yourColorPalette];
- iOS 7+
UIImageView-AGCInitials is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod "UIImageView-AGCInitials"
Copy and paste AGCInitialsColors.h/m and UIImageView+AGCInitials.h/m classes into your project.
To run the example project, clone the repo, and run pod install
from the Example directory first.
You can even try the example online with Appetize
Andrea Cipriani, [email protected] - Twitter @AndreaCipriani
UIImageView-AGCInitials is available under the MIT license. See the LICENSE file for more info.