Skip to content

Create an image with the initials of a contact's name. Objective-C category on UIImageView.

License

Notifications You must be signed in to change notification settings

andreacipriani/UIImageView-AGCInitials

Repository files navigation

UIImageView-AGCInitials

CI Status Version License Platform

AGCInitials Example screenshot 1 AGCInitials Example screenshot 2

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.

Features

  • 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 💪

How to use it - Basic

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:

Mick Jagger initials example

How to use it - Details

  • 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;

MJ initials example MJ initials example

  • 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:

Mick Jagger initials example Michael Jackson initials example

  • 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];

Requirements

  • iOS 7+

Installation with Cocoapods

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

pod "UIImageView-AGCInitials"

Installation without Cocoapods

Copy and paste AGCInitialsColors.h/m and UIImageView+AGCInitials.h/m classes into your project.

Run the example

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

Author

Andrea Cipriani, [email protected] - Twitter @AndreaCipriani

License

UIImageView-AGCInitials is available under the MIT license. See the LICENSE file for more info.

About

Create an image with the initials of a contact's name. Objective-C category on UIImageView.

Resources

License

Stars

Watchers

Forks

Packages

No packages published