Skip to content

Commit 4a6a12f

Browse files
committed
Remove file redundancy; formatting fixes
1 parent df13130 commit 4a6a12f

7 files changed

Lines changed: 31 additions & 252 deletions

File tree

UIImageView+Letters/UIImageView+Letters.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,8 @@
6060
@param string The string used to generate the initials. This should be a user's full name if available
6161
@param color (optional) This optional paramter sets the background of the image. If not provided, a random color will be generated
6262
@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
63+
@param fontName This will use a custom font attribute. If not provided, it will default to system font
6464
*/
65-
- (void)setImageWithString:(NSString *)string color:(UIColor *)color circular:(BOOL)isCircular fontWithName: (NSString *) fontName;
65+
- (void)setImageWithString:(NSString *)string color:(UIColor *)color circular:(BOOL)isCircular fontName:(NSString *)fontName;
6666

6767
@end

UIImageView+Letters/UIImageView+Letters.m

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,27 +24,30 @@
2424

2525
#import "UIImageView+Letters.h"
2626

27+
// This multiplier sets the font size based on the view bounds
28+
static const CGFloat kFontResizingProportion = 0.48f;
29+
2730
@interface UIImageView (LettersPrivate)
2831

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

3134
@end
3235

3336
@implementation UIImageView (Letters)
3437

3538
- (void)setImageWithString:(NSString *)string {
36-
[self setImageWithString:string color:nil circular:NO];
39+
[self setImageWithString:string color:nil circular:NO fontName:nil];
3740
}
3841

3942
- (void)setImageWithString:(NSString *)string color:(UIColor *)color {
40-
[self setImageWithString:string color:color circular:NO];
43+
[self setImageWithString:string color:color circular:NO fontName:nil];
4144
}
4245

4346
- (void)setImageWithString:(NSString *)string color:(UIColor *)color circular:(BOOL)isCircular {
44-
[self setImageWithString:string color:color circular:isCircular fontWithName:nil];
47+
[self setImageWithString:string color:color circular:isCircular fontName:nil];
4548
}
4649

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

5053
NSMutableArray *words = [[string componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] mutableCopy];
@@ -61,12 +64,12 @@ - (void)setImageWithString:(NSString *)string color:(UIColor *)color circular:(B
6164
if ([words count] >= 2) {
6265
NSString *lastWord = [words lastObject];
6366

64-
while([lastWord length] == 0 && [words count] >= 2) {
67+
while ([lastWord length] == 0 && [words count] >= 2) {
6568
[words removeLastObject];
6669
lastWord = [words lastObject];
6770
}
6871

69-
if([words count] > 1) {
72+
if ([words count] > 1) {
7073
[displayString appendString:[lastWord substringToIndex:1]];
7174
}
7275
}
@@ -79,12 +82,14 @@ - (void)setImageWithString:(NSString *)string color:(UIColor *)color circular:(B
7982

8083
#pragma mark - Helpers
8184

82-
- (UIFont *)fontForText: (NSString *) fontName {
85+
- (UIFont *)fontForText:(NSString *)fontName {
8386

87+
CGFloat fontSize = CGRectGetWidth(self.bounds) * kFontResizingProportion;
8488
if (fontName) {
85-
return [UIFont fontWithName:fontName size:CGRectGetWidth(self.bounds) * 0.48];
86-
} else {
87-
return [UIFont systemFontOfSize:CGRectGetWidth(self.bounds) * 0.48];
89+
return [UIFont fontWithName:fontName size:fontSize];
90+
}
91+
else {
92+
return [UIFont systemFontOfSize:fontSize];
8893
}
8994

9095
}
@@ -109,7 +114,7 @@ - (UIColor *)randomColor {
109114
return [UIColor colorWithRed:red green:green blue:blue alpha:1.0f];
110115
}
111116

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

114119
CGFloat scale = [UIScreen mainScreen].scale;
115120

@@ -149,7 +154,7 @@ - (UIImage *)imageSnapshotFromText:(NSString *)text backgroundColor:(UIColor *)c
149154
CGSize textSize = [text sizeWithAttributes:@{NSFontAttributeName:[self fontForText: fontName]}];
150155
CGRect bounds = self.bounds;
151156
[text drawInRect:CGRectMake(bounds.size.width/2 - textSize.width/2, bounds.size.height/2 - textSize.height/2, textSize.width, textSize.height)
152-
withAttributes:@{NSFontAttributeName:[self fontForText: fontName], NSForegroundColorAttributeName:[UIColor whiteColor]}];
157+
withAttributes:@{NSFontAttributeName:[self fontForText:fontName], NSForegroundColorAttributeName:[UIColor whiteColor]}];
153158

154159
UIImage *snapshot = UIGraphicsGetImageFromCurrentImageContext();
155160
UIGraphicsEndImageContext();

UIImageViewLettersSample/UIImageViewLettersSample.xcodeproj/project.pbxproj

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414
C855D2F21A00593300A5B693 /* Images.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = C855D2F11A00593300A5B693 /* Images.xcassets */; };
1515
C855D2F51A00593300A5B693 /* LaunchScreen.xib in Resources */ = {isa = PBXBuildFile; fileRef = C855D2F31A00593300A5B693 /* LaunchScreen.xib */; };
1616
C855D3011A00593300A5B693 /* UIImageViewLettersSampleTests.m in Sources */ = {isa = PBXBuildFile; fileRef = C855D3001A00593300A5B693 /* UIImageViewLettersSampleTests.m */; };
17-
C855D30D1A0059F200A5B693 /* UIImageView+Letters.m in Sources */ = {isa = PBXBuildFile; fileRef = C855D30C1A0059F200A5B693 /* UIImageView+Letters.m */; };
1817
C855D30F1A005B1600A5B693 /* QuartzCore.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = C855D30E1A005B1600A5B693 /* QuartzCore.framework */; };
18+
C86C71DC1ACF0731007BD322 /* UIImageView+Letters.m in Sources */ = {isa = PBXBuildFile; fileRef = C86C71DB1ACF0731007BD322 /* UIImageView+Letters.m */; };
1919
/* End PBXBuildFile section */
2020

2121
/* Begin PBXContainerItemProxy section */
@@ -42,9 +42,9 @@
4242
C855D2FA1A00593300A5B693 /* UIImageViewLettersSampleTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = UIImageViewLettersSampleTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
4343
C855D2FF1A00593300A5B693 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
4444
C855D3001A00593300A5B693 /* UIImageViewLettersSampleTests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = UIImageViewLettersSampleTests.m; sourceTree = "<group>"; };
45-
C855D30B1A0059F200A5B693 /* UIImageView+Letters.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "UIImageView+Letters.h"; sourceTree = "<group>"; };
46-
C855D30C1A0059F200A5B693 /* UIImageView+Letters.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "UIImageView+Letters.m"; sourceTree = "<group>"; };
4745
C855D30E1A005B1600A5B693 /* QuartzCore.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = QuartzCore.framework; path = System/Library/Frameworks/QuartzCore.framework; sourceTree = SDKROOT; };
46+
C86C71DA1ACF0731007BD322 /* UIImageView+Letters.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "UIImageView+Letters.h"; sourceTree = "<group>"; };
47+
C86C71DB1ACF0731007BD322 /* UIImageView+Letters.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "UIImageView+Letters.m"; sourceTree = "<group>"; };
4848
/* End PBXFileReference section */
4949

5050
/* Begin PBXFrameworksBuildPhase section */
@@ -88,7 +88,7 @@
8888
C855D2E31A00593300A5B693 /* UIImageViewLettersSample */ = {
8989
isa = PBXGroup;
9090
children = (
91-
C855D30A1A0059F200A5B693 /* UIImageView+Letters */,
91+
C86C71D91ACF0731007BD322 /* UIImageView+Letters */,
9292
C855D2E81A00593300A5B693 /* AppDelegate.h */,
9393
C855D2E91A00593300A5B693 /* AppDelegate.m */,
9494
C855D2EB1A00593300A5B693 /* ViewController.h */,
@@ -127,13 +127,14 @@
127127
name = "Supporting Files";
128128
sourceTree = "<group>";
129129
};
130-
C855D30A1A0059F200A5B693 /* UIImageView+Letters */ = {
130+
C86C71D91ACF0731007BD322 /* UIImageView+Letters */ = {
131131
isa = PBXGroup;
132132
children = (
133-
C855D30B1A0059F200A5B693 /* UIImageView+Letters.h */,
134-
C855D30C1A0059F200A5B693 /* UIImageView+Letters.m */,
133+
C86C71DA1ACF0731007BD322 /* UIImageView+Letters.h */,
134+
C86C71DB1ACF0731007BD322 /* UIImageView+Letters.m */,
135135
);
136-
path = "UIImageView+Letters";
136+
name = "UIImageView+Letters";
137+
path = "../../UIImageView+Letters";
137138
sourceTree = "<group>";
138139
};
139140
/* End PBXGroup section */
@@ -238,7 +239,7 @@
238239
files = (
239240
C855D2ED1A00593300A5B693 /* ViewController.m in Sources */,
240241
C855D2EA1A00593300A5B693 /* AppDelegate.m in Sources */,
241-
C855D30D1A0059F200A5B693 /* UIImageView+Letters.m in Sources */,
242+
C86C71DC1ACF0731007BD322 /* UIImageView+Letters.m in Sources */,
242243
C855D2E71A00593300A5B693 /* main.m in Sources */,
243244
);
244245
runOnlyForDeploymentPostprocessing = 0;

UIImageViewLettersSample/UIImageViewLettersSample/UIImageView+Letters/UIImageView+Letters.h

Lines changed: 0 additions & 67 deletions
This file was deleted.

UIImageViewLettersSample/UIImageViewLettersSample/UIImageView+Letters/UIImageView+Letters.m

Lines changed: 0 additions & 160 deletions
This file was deleted.

UIImageViewLettersSample/UIImageViewLettersSample/ViewController.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ - (void)didReceiveMemoryWarning {
3939
- (IBAction)refreshSampleImage:(id)sender {
4040

4141
if (_useFuturaSwitch.isOn) {
42-
[_sampleImageView setImageWithString:_nameField.text color:nil circular:_circularSwitch.isOn fontWithName:@"Futura-Medium"];
42+
[_sampleImageView setImageWithString:_nameField.text color:nil circular:_circularSwitch.isOn fontName:@"Futura-Medium"];
4343
} else {
4444
[_sampleImageView setImageWithString:_nameField.text color:nil circular:_circularSwitch.isOn];
4545
}

0 commit comments

Comments
 (0)