Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,16 @@ Call the following methods on any `UIImageView` instance to set the image:
+ `- (void)setImageWithString:(NSString *)string`
+ `- (void)setImageWithString:(NSString *)string color:(UIColor *)color`
+ `- (void)setImageWithString:(NSString *)string color:(UIColor *)color circular:(BOOL)isCircular`
+ `- (void)setImageWithString:(NSString *)string color:(UIColor *)color circular:(BOOL)isCircular fontWithName: (NSString *) fontName`

`string` is the string used to generate the initials. This should be a user's full name if available.

`color` is an optional parameter that sets the background color of the image. Pass in `nil` to have a color automatically generated for you.

`isCircular` is a boolean parameter that will automatically clip the image to a circle if enabled.

`fontName` is a NSString that specifies a custom font. See all String Identifiers for built in iOS fonts [here](http://iosfonts.com).

##### Example

```
Expand Down
10 changes: 10 additions & 0 deletions UIImageView+Letters/UIImageView+Letters.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,14 @@
*/
- (void)setImageWithString:(NSString *)string color:(UIColor *)color circular:(BOOL)isCircular;

/**
Sets the image property of the view based on initial text, a specified background color, a custom font, and a circular clipping

@param string The string used to generate the initials. This should be a user's full name if available
@param color (optional) This optional paramter sets the background of the image. If not provided, a random color will be generated
@param isCircular This boolean will determine if the image view will be clipped to a circular shape
@param fontName will use a custom font rather than System Font
*/
- (void)setImageWithString:(NSString *)string color:(UIColor *)color circular:(BOOL)isCircular fontWithName: (NSString *) fontName;

@end
24 changes: 17 additions & 7 deletions UIImageView+Letters/UIImageView+Letters.m
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

@interface UIImageView (LettersPrivate)

- (UIImage *)imageSnapshotFromText:(NSString *)text backgroundColor:(UIColor *)color circular:(BOOL)isCircular;
- (UIImage *)imageSnapshotFromText:(NSString *)text backgroundColor:(UIColor *)color circular:(BOOL)isCircular fontName: (NSString*) fontName;

@end

Expand All @@ -41,6 +41,10 @@ - (void)setImageWithString:(NSString *)string color:(UIColor *)color {
}

- (void)setImageWithString:(NSString *)string color:(UIColor *)color circular:(BOOL)isCircular {
[self setImageWithString:string color:color circular:isCircular fontWithName:nil];
}

- (void)setImageWithString:(NSString *)string color:(UIColor *)color circular:(BOOL)isCircular fontWithName: (NSString *) fontName {
NSMutableString *displayString = [NSMutableString stringWithString:@""];

NSMutableArray *words = [[string componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] mutableCopy];
Expand Down Expand Up @@ -70,13 +74,19 @@ - (void)setImageWithString:(NSString *)string color:(UIColor *)color circular:(B

UIColor *backgroundColor = color ? color : [self randomColor];

self.image = [self imageSnapshotFromText:[displayString uppercaseString] backgroundColor:backgroundColor circular:isCircular];
self.image = [self imageSnapshotFromText:[displayString uppercaseString] backgroundColor:backgroundColor circular:isCircular fontName:fontName];
}

#pragma mark - Helpers

- (UIFont *)fontForText {
return [UIFont systemFontOfSize:CGRectGetWidth(self.bounds) * 0.48];
- (UIFont *)fontForText: (NSString *) fontName {

if (fontName) {
return [UIFont fontWithName:fontName size:CGRectGetWidth(self.bounds) * 0.48];
} else {
return [UIFont systemFontOfSize:CGRectGetWidth(self.bounds) * 0.48];
}

}

- (UIColor *)randomColor {
Expand All @@ -99,7 +109,7 @@ - (UIColor *)randomColor {
return [UIColor colorWithRed:red green:green blue:blue alpha:1.0f];
}

- (UIImage *)imageSnapshotFromText:(NSString *)text backgroundColor:(UIColor *)color circular:(BOOL)isCircular {
- (UIImage *)imageSnapshotFromText:(NSString *)text backgroundColor:(UIColor *)color circular:(BOOL)isCircular fontName:(NSString *)fontName{

CGFloat scale = [UIScreen mainScreen].scale;

Expand Down Expand Up @@ -136,10 +146,10 @@ - (UIImage *)imageSnapshotFromText:(NSString *)text backgroundColor:(UIColor *)c
//
// Draw text in the context
//
CGSize textSize = [text sizeWithAttributes:@{NSFontAttributeName:[self fontForText]}];
CGSize textSize = [text sizeWithAttributes:@{NSFontAttributeName:[self fontForText: fontName]}];
CGRect bounds = self.bounds;
[text drawInRect:CGRectMake(bounds.size.width/2 - textSize.width/2, bounds.size.height/2 - textSize.height/2, textSize.width, textSize.height)
withAttributes:@{NSFontAttributeName:[self fontForText], NSForegroundColorAttributeName:[UIColor whiteColor]}];
withAttributes:@{NSFontAttributeName:[self fontForText: fontName], NSForegroundColorAttributeName:[UIColor whiteColor]}];

UIImage *snapshot = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>IDESourceControlProjectFavoriteDictionaryKey</key>
<false/>
<key>IDESourceControlProjectIdentifier</key>
<string>A3F1ED25-0928-4526-9FF8-C73C2AFAA60A</string>
<key>IDESourceControlProjectName</key>
<string>UIImageViewLettersSample</string>
<key>IDESourceControlProjectOriginsDictionary</key>
<dict>
<key>9FFD21C6A65AE22198CFA6E939DC569037B4263C</key>
<string>github.com:MattFaluotico/UIImageView-Letters.git</string>
</dict>
<key>IDESourceControlProjectPath</key>
<string>UIImageViewLettersSample/UIImageViewLettersSample.xcodeproj</string>
<key>IDESourceControlProjectRelativeInstallPathDictionary</key>
<dict>
<key>9FFD21C6A65AE22198CFA6E939DC569037B4263C</key>
<string>../../..</string>
</dict>
<key>IDESourceControlProjectURL</key>
<string>github.com:MattFaluotico/UIImageView-Letters.git</string>
<key>IDESourceControlProjectVersion</key>
<integer>111</integer>
<key>IDESourceControlProjectWCCIdentifier</key>
<string>9FFD21C6A65AE22198CFA6E939DC569037B4263C</string>
<key>IDESourceControlProjectWCConfigurations</key>
<array>
<dict>
<key>IDESourceControlRepositoryExtensionIdentifierKey</key>
<string>public.vcs.git</string>
<key>IDESourceControlWCCIdentifierKey</key>
<string>9FFD21C6A65AE22198CFA6E939DC569037B4263C</string>
<key>IDESourceControlWCCName</key>
<string>UIImageView-Letters</string>
</dict>
</array>
</dict>
</plist>
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "0620"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
buildImplicitDependencies = "YES">
<BuildActionEntries>
<BuildActionEntry
buildForTesting = "YES"
buildForRunning = "YES"
buildForProfiling = "YES"
buildForArchiving = "YES"
buildForAnalyzing = "YES">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "C855D2E01A00593300A5B693"
BuildableName = "UIImageViewLettersSample.app"
BlueprintName = "UIImageViewLettersSample"
ReferencedContainer = "container:UIImageViewLettersSample.xcodeproj">
</BuildableReference>
</BuildActionEntry>
<BuildActionEntry
buildForTesting = "YES"
buildForRunning = "YES"
buildForProfiling = "NO"
buildForArchiving = "NO"
buildForAnalyzing = "YES">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "C855D2F91A00593300A5B693"
BuildableName = "UIImageViewLettersSampleTests.xctest"
BlueprintName = "UIImageViewLettersSampleTests"
ReferencedContainer = "container:UIImageViewLettersSample.xcodeproj">
</BuildableReference>
</BuildActionEntry>
</BuildActionEntries>
</BuildAction>
<TestAction
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
shouldUseLaunchSchemeArgsEnv = "YES"
buildConfiguration = "Debug">
<Testables>
<TestableReference
skipped = "NO">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "C855D2F91A00593300A5B693"
BuildableName = "UIImageViewLettersSampleTests.xctest"
BlueprintName = "UIImageViewLettersSampleTests"
ReferencedContainer = "container:UIImageViewLettersSample.xcodeproj">
</BuildableReference>
</TestableReference>
</Testables>
<MacroExpansion>
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "C855D2E01A00593300A5B693"
BuildableName = "UIImageViewLettersSample.app"
BlueprintName = "UIImageViewLettersSample"
ReferencedContainer = "container:UIImageViewLettersSample.xcodeproj">
</BuildableReference>
</MacroExpansion>
</TestAction>
<LaunchAction
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
launchStyle = "0"
useCustomWorkingDirectory = "NO"
buildConfiguration = "Debug"
ignoresPersistentStateOnLaunch = "NO"
debugDocumentVersioning = "YES"
allowLocationSimulation = "YES">
<BuildableProductRunnable
runnableDebuggingMode = "0">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "C855D2E01A00593300A5B693"
BuildableName = "UIImageViewLettersSample.app"
BlueprintName = "UIImageViewLettersSample"
ReferencedContainer = "container:UIImageViewLettersSample.xcodeproj">
</BuildableReference>
</BuildableProductRunnable>
<AdditionalOptions>
</AdditionalOptions>
</LaunchAction>
<ProfileAction
shouldUseLaunchSchemeArgsEnv = "YES"
savedToolIdentifier = ""
useCustomWorkingDirectory = "NO"
buildConfiguration = "Release"
debugDocumentVersioning = "YES">
<BuildableProductRunnable
runnableDebuggingMode = "0">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "C855D2E01A00593300A5B693"
BuildableName = "UIImageViewLettersSample.app"
BlueprintName = "UIImageViewLettersSample"
ReferencedContainer = "container:UIImageViewLettersSample.xcodeproj">
</BuildableReference>
</BuildableProductRunnable>
</ProfileAction>
<AnalyzeAction
buildConfiguration = "Debug">
</AnalyzeAction>
<ArchiveAction
buildConfiguration = "Release"
revealArchiveInOrganizer = "YES">
</ArchiveAction>
</Scheme>
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>SchemeUserState</key>
<dict>
<key>UIImageViewLettersSample.xcscheme</key>
<dict>
<key>orderHint</key>
<integer>0</integer>
</dict>
</dict>
<key>SuppressBuildableAutocreation</key>
<dict>
<key>C855D2E01A00593300A5B693</key>
<dict>
<key>primary</key>
<true/>
</dict>
<key>C855D2F91A00593300A5B693</key>
<dict>
<key>primary</key>
<true/>
</dict>
</dict>
</dict>
</plist>
Loading