Skip to content

Commit df13130

Browse files
committed
Merge pull request #11 from MattFaluotico/master
Custom font option
2 parents a027c5e + 4ce7382 commit df13130

11 files changed

Lines changed: 307 additions & 39 deletions

File tree

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,16 @@ Call the following methods on any `UIImageView` instance to set the image:
3333
+ `- (void)setImageWithString:(NSString *)string`
3434
+ `- (void)setImageWithString:(NSString *)string color:(UIColor *)color`
3535
+ `- (void)setImageWithString:(NSString *)string color:(UIColor *)color circular:(BOOL)isCircular`
36+
+ `- (void)setImageWithString:(NSString *)string color:(UIColor *)color circular:(BOOL)isCircular fontWithName: (NSString *) fontName`
3637

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

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

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

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

4548
```

UIImageView+Letters/UIImageView+Letters.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,4 +54,14 @@
5454
*/
5555
- (void)setImageWithString:(NSString *)string color:(UIColor *)color circular:(BOOL)isCircular;
5656

57+
/**
58+
Sets the image property of the view based on initial text, a specified background color, a custom font, and a circular clipping
59+
60+
@param string The string used to generate the initials. This should be a user's full name if available
61+
@param color (optional) This optional paramter sets the background of the image. If not provided, a random color will be generated
62+
@param isCircular This boolean will determine if the image view will be clipped to a circular shape
63+
@param fontName will use a custom font rather than System Font
64+
*/
65+
- (void)setImageWithString:(NSString *)string color:(UIColor *)color circular:(BOOL)isCircular fontWithName: (NSString *) fontName;
66+
5767
@end

UIImageView+Letters/UIImageView+Letters.m

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626

2727
@interface UIImageView (LettersPrivate)
2828

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

3131
@end
3232

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

4343
- (void)setImageWithString:(NSString *)string color:(UIColor *)color circular:(BOOL)isCircular {
44+
[self setImageWithString:string color:color circular:isCircular fontWithName:nil];
45+
}
46+
47+
- (void)setImageWithString:(NSString *)string color:(UIColor *)color circular:(BOOL)isCircular fontWithName: (NSString *) fontName {
4448
NSMutableString *displayString = [NSMutableString stringWithString:@""];
4549

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

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

73-
self.image = [self imageSnapshotFromText:[displayString uppercaseString] backgroundColor:backgroundColor circular:isCircular];
77+
self.image = [self imageSnapshotFromText:[displayString uppercaseString] backgroundColor:backgroundColor circular:isCircular fontName:fontName];
7478
}
7579

7680
#pragma mark - Helpers
7781

78-
- (UIFont *)fontForText {
79-
return [UIFont systemFontOfSize:CGRectGetWidth(self.bounds) * 0.48];
82+
- (UIFont *)fontForText: (NSString *) fontName {
83+
84+
if (fontName) {
85+
return [UIFont fontWithName:fontName size:CGRectGetWidth(self.bounds) * 0.48];
86+
} else {
87+
return [UIFont systemFontOfSize:CGRectGetWidth(self.bounds) * 0.48];
88+
}
89+
8090
}
8191

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

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

104114
CGFloat scale = [UIScreen mainScreen].scale;
105115

@@ -136,10 +146,10 @@ - (UIImage *)imageSnapshotFromText:(NSString *)text backgroundColor:(UIColor *)c
136146
//
137147
// Draw text in the context
138148
//
139-
CGSize textSize = [text sizeWithAttributes:@{NSFontAttributeName:[self fontForText]}];
149+
CGSize textSize = [text sizeWithAttributes:@{NSFontAttributeName:[self fontForText: fontName]}];
140150
CGRect bounds = self.bounds;
141151
[text drawInRect:CGRectMake(bounds.size.width/2 - textSize.width/2, bounds.size.height/2 - textSize.height/2, textSize.width, textSize.height)
142-
withAttributes:@{NSFontAttributeName:[self fontForText], NSForegroundColorAttributeName:[UIColor whiteColor]}];
152+
withAttributes:@{NSFontAttributeName:[self fontForText: fontName], NSForegroundColorAttributeName:[UIColor whiteColor]}];
143153

144154
UIImage *snapshot = UIGraphicsGetImageFromCurrentImageContext();
145155
UIGraphicsEndImageContext();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
3+
<plist version="1.0">
4+
<dict>
5+
<key>IDESourceControlProjectFavoriteDictionaryKey</key>
6+
<false/>
7+
<key>IDESourceControlProjectIdentifier</key>
8+
<string>A3F1ED25-0928-4526-9FF8-C73C2AFAA60A</string>
9+
<key>IDESourceControlProjectName</key>
10+
<string>UIImageViewLettersSample</string>
11+
<key>IDESourceControlProjectOriginsDictionary</key>
12+
<dict>
13+
<key>9FFD21C6A65AE22198CFA6E939DC569037B4263C</key>
14+
<string>github.com:MattFaluotico/UIImageView-Letters.git</string>
15+
</dict>
16+
<key>IDESourceControlProjectPath</key>
17+
<string>UIImageViewLettersSample/UIImageViewLettersSample.xcodeproj</string>
18+
<key>IDESourceControlProjectRelativeInstallPathDictionary</key>
19+
<dict>
20+
<key>9FFD21C6A65AE22198CFA6E939DC569037B4263C</key>
21+
<string>../../..</string>
22+
</dict>
23+
<key>IDESourceControlProjectURL</key>
24+
<string>github.com:MattFaluotico/UIImageView-Letters.git</string>
25+
<key>IDESourceControlProjectVersion</key>
26+
<integer>111</integer>
27+
<key>IDESourceControlProjectWCCIdentifier</key>
28+
<string>9FFD21C6A65AE22198CFA6E939DC569037B4263C</string>
29+
<key>IDESourceControlProjectWCConfigurations</key>
30+
<array>
31+
<dict>
32+
<key>IDESourceControlRepositoryExtensionIdentifierKey</key>
33+
<string>public.vcs.git</string>
34+
<key>IDESourceControlWCCIdentifierKey</key>
35+
<string>9FFD21C6A65AE22198CFA6E939DC569037B4263C</string>
36+
<key>IDESourceControlWCCName</key>
37+
<string>UIImageView-Letters</string>
38+
</dict>
39+
</array>
40+
</dict>
41+
</plist>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<Scheme
3+
LastUpgradeVersion = "0620"
4+
version = "1.3">
5+
<BuildAction
6+
parallelizeBuildables = "YES"
7+
buildImplicitDependencies = "YES">
8+
<BuildActionEntries>
9+
<BuildActionEntry
10+
buildForTesting = "YES"
11+
buildForRunning = "YES"
12+
buildForProfiling = "YES"
13+
buildForArchiving = "YES"
14+
buildForAnalyzing = "YES">
15+
<BuildableReference
16+
BuildableIdentifier = "primary"
17+
BlueprintIdentifier = "C855D2E01A00593300A5B693"
18+
BuildableName = "UIImageViewLettersSample.app"
19+
BlueprintName = "UIImageViewLettersSample"
20+
ReferencedContainer = "container:UIImageViewLettersSample.xcodeproj">
21+
</BuildableReference>
22+
</BuildActionEntry>
23+
<BuildActionEntry
24+
buildForTesting = "YES"
25+
buildForRunning = "YES"
26+
buildForProfiling = "NO"
27+
buildForArchiving = "NO"
28+
buildForAnalyzing = "YES">
29+
<BuildableReference
30+
BuildableIdentifier = "primary"
31+
BlueprintIdentifier = "C855D2F91A00593300A5B693"
32+
BuildableName = "UIImageViewLettersSampleTests.xctest"
33+
BlueprintName = "UIImageViewLettersSampleTests"
34+
ReferencedContainer = "container:UIImageViewLettersSample.xcodeproj">
35+
</BuildableReference>
36+
</BuildActionEntry>
37+
</BuildActionEntries>
38+
</BuildAction>
39+
<TestAction
40+
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
41+
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
42+
shouldUseLaunchSchemeArgsEnv = "YES"
43+
buildConfiguration = "Debug">
44+
<Testables>
45+
<TestableReference
46+
skipped = "NO">
47+
<BuildableReference
48+
BuildableIdentifier = "primary"
49+
BlueprintIdentifier = "C855D2F91A00593300A5B693"
50+
BuildableName = "UIImageViewLettersSampleTests.xctest"
51+
BlueprintName = "UIImageViewLettersSampleTests"
52+
ReferencedContainer = "container:UIImageViewLettersSample.xcodeproj">
53+
</BuildableReference>
54+
</TestableReference>
55+
</Testables>
56+
<MacroExpansion>
57+
<BuildableReference
58+
BuildableIdentifier = "primary"
59+
BlueprintIdentifier = "C855D2E01A00593300A5B693"
60+
BuildableName = "UIImageViewLettersSample.app"
61+
BlueprintName = "UIImageViewLettersSample"
62+
ReferencedContainer = "container:UIImageViewLettersSample.xcodeproj">
63+
</BuildableReference>
64+
</MacroExpansion>
65+
</TestAction>
66+
<LaunchAction
67+
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
68+
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
69+
launchStyle = "0"
70+
useCustomWorkingDirectory = "NO"
71+
buildConfiguration = "Debug"
72+
ignoresPersistentStateOnLaunch = "NO"
73+
debugDocumentVersioning = "YES"
74+
allowLocationSimulation = "YES">
75+
<BuildableProductRunnable
76+
runnableDebuggingMode = "0">
77+
<BuildableReference
78+
BuildableIdentifier = "primary"
79+
BlueprintIdentifier = "C855D2E01A00593300A5B693"
80+
BuildableName = "UIImageViewLettersSample.app"
81+
BlueprintName = "UIImageViewLettersSample"
82+
ReferencedContainer = "container:UIImageViewLettersSample.xcodeproj">
83+
</BuildableReference>
84+
</BuildableProductRunnable>
85+
<AdditionalOptions>
86+
</AdditionalOptions>
87+
</LaunchAction>
88+
<ProfileAction
89+
shouldUseLaunchSchemeArgsEnv = "YES"
90+
savedToolIdentifier = ""
91+
useCustomWorkingDirectory = "NO"
92+
buildConfiguration = "Release"
93+
debugDocumentVersioning = "YES">
94+
<BuildableProductRunnable
95+
runnableDebuggingMode = "0">
96+
<BuildableReference
97+
BuildableIdentifier = "primary"
98+
BlueprintIdentifier = "C855D2E01A00593300A5B693"
99+
BuildableName = "UIImageViewLettersSample.app"
100+
BlueprintName = "UIImageViewLettersSample"
101+
ReferencedContainer = "container:UIImageViewLettersSample.xcodeproj">
102+
</BuildableReference>
103+
</BuildableProductRunnable>
104+
</ProfileAction>
105+
<AnalyzeAction
106+
buildConfiguration = "Debug">
107+
</AnalyzeAction>
108+
<ArchiveAction
109+
buildConfiguration = "Release"
110+
revealArchiveInOrganizer = "YES">
111+
</ArchiveAction>
112+
</Scheme>
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
3+
<plist version="1.0">
4+
<dict>
5+
<key>SchemeUserState</key>
6+
<dict>
7+
<key>UIImageViewLettersSample.xcscheme</key>
8+
<dict>
9+
<key>orderHint</key>
10+
<integer>0</integer>
11+
</dict>
12+
</dict>
13+
<key>SuppressBuildableAutocreation</key>
14+
<dict>
15+
<key>C855D2E01A00593300A5B693</key>
16+
<dict>
17+
<key>primary</key>
18+
<true/>
19+
</dict>
20+
<key>C855D2F91A00593300A5B693</key>
21+
<dict>
22+
<key>primary</key>
23+
<true/>
24+
</dict>
25+
</dict>
26+
</dict>
27+
</plist>

0 commit comments

Comments
 (0)