Skip to content

Commit 2977621

Browse files
committed
Merge pull request #5 from caseybrichardson/master
Fix for two issues
2 parents 423de0c + fd5de18 commit 2977621

2 files changed

Lines changed: 68 additions & 68 deletions

File tree

UIImageView+Letters/UIImageView+Letters.m

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -31,54 +31,41 @@ - (void)setImageWithString:(NSString *)string {
3131
}
3232

3333
- (void)setImageWithString:(NSString *)string color:(UIColor *)color {
34-
35-
//
36-
// Set up a temporary view to contain the text label
37-
//
38-
UIView *tempView = [[UIView alloc] initWithFrame:self.bounds];
39-
40-
UILabel *letterLabel = [[UILabel alloc] initWithFrame:self.bounds];
41-
letterLabel.textAlignment = NSTextAlignmentCenter;
42-
letterLabel.backgroundColor = [UIColor clearColor];
43-
letterLabel.textColor = [UIColor whiteColor];
44-
letterLabel.adjustsFontSizeToFitWidth = YES;
45-
letterLabel.minimumScaleFactor = 8.0f / 65.0f;
46-
letterLabel.font = [self fontForLetterLabel];
47-
[tempView addSubview:letterLabel];
48-
4934
NSMutableString *displayString = [NSMutableString stringWithString:@""];
5035

51-
NSArray *words = [string componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
36+
NSMutableArray *words = [[string componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] mutableCopy];
5237

38+
//
39+
// Get first letter of the first and last word
40+
//
5341
if ([words count]) {
5442
NSString *firstWord = [words firstObject];
55-
if ([firstWord length]) {
43+
if ([firstWord length] != 0) {
5644
[displayString appendString:[firstWord substringToIndex:1]];
5745
}
5846

5947
if ([words count] >= 2) {
6048
NSString *lastWord = [words lastObject];
61-
if ([lastWord length]) {
49+
50+
while([lastWord length] == 0 && [words count] >= 2) {
51+
[words removeLastObject];
52+
lastWord = [words lastObject];
53+
}
54+
55+
if([words count] > 1) {
6256
[displayString appendString:[lastWord substringToIndex:1]];
6357
}
6458
}
6559
}
66-
letterLabel.text = [displayString uppercaseString];
6760

68-
//
69-
// Set the background color
70-
//
71-
tempView.backgroundColor = color ? color : [self randomColor];
61+
UIColor *backgroundColor = color ? color : [self randomColor];
7262

73-
//
74-
// Return an image instance of the temporary view
75-
//
76-
self.image = [self imageSnapshotFromView:tempView];
63+
self.image = [self imageSnapshotFromText:[displayString uppercaseString] backgroundColor:backgroundColor];
7764
}
7865

7966
#pragma mark - Helpers
8067

81-
- (UIFont *)fontForLetterLabel {
68+
- (UIFont *)fontForText {
8269
return [UIFont systemFontOfSize:CGRectGetWidth(self.bounds) * 0.48];
8370
}
8471

@@ -102,7 +89,7 @@ - (UIColor *)randomColor {
10289
return [UIColor colorWithRed:red green:green blue:blue alpha:1.0f];
10390
}
10491

105-
- (UIImage *)imageSnapshotFromView:(UIView *)inputView {
92+
- (UIImage *)imageSnapshotFromText:(NSString *)text backgroundColor:(UIColor *)color {
10693

10794
CGFloat scale = [UIScreen mainScreen].scale;
10895

@@ -116,11 +103,24 @@ - (UIImage *)imageSnapshotFromView:(UIView *)inputView {
116103
size.height = floorf(size.height * scale) / scale;
117104
}
118105

119-
UIGraphicsBeginImageContextWithOptions(size, YES, scale);
106+
UIGraphicsBeginImageContextWithOptions(size, NO, scale);
107+
120108
CGContextRef context = UIGraphicsGetCurrentContext();
121-
CGContextTranslateCTM(context, -self.bounds.origin.x, -self.bounds.origin.y);
122109

123-
[inputView.layer renderInContext:context];
110+
//
111+
// Fill background of context
112+
//
113+
CGContextSetFillColorWithColor(context, color.CGColor);
114+
CGContextFillRect(context, CGRectMake(0, 0, size.width, size.height));
115+
116+
//
117+
// Draw text in the context
118+
//
119+
CGSize textSize = [text sizeWithAttributes:@{NSFontAttributeName:[self fontForText]}];
120+
CGRect bounds = self.bounds;
121+
[text drawInRect:CGRectMake(bounds.size.width/2 - textSize.width/2, bounds.size.height/2 - textSize.height/2, textSize.width, textSize.height)
122+
withAttributes:@{NSFontAttributeName:[self fontForText], NSForegroundColorAttributeName:[UIColor whiteColor]}];
123+
124124
UIImage *snapshot = UIGraphicsGetImageFromCurrentImageContext();
125125
UIGraphicsEndImageContext();
126126

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

Lines changed: 36 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -31,54 +31,41 @@ - (void)setImageWithString:(NSString *)string {
3131
}
3232

3333
- (void)setImageWithString:(NSString *)string color:(UIColor *)color {
34-
35-
//
36-
// Set up a temporary view to contain the text label
37-
//
38-
UIView *tempView = [[UIView alloc] initWithFrame:self.bounds];
39-
40-
UILabel *letterLabel = [[UILabel alloc] initWithFrame:self.bounds];
41-
letterLabel.textAlignment = NSTextAlignmentCenter;
42-
letterLabel.backgroundColor = [UIColor clearColor];
43-
letterLabel.textColor = [UIColor whiteColor];
44-
letterLabel.adjustsFontSizeToFitWidth = YES;
45-
letterLabel.minimumScaleFactor = 8.0f / 65.0f;
46-
letterLabel.font = [self fontForLetterLabel];
47-
[tempView addSubview:letterLabel];
48-
4934
NSMutableString *displayString = [NSMutableString stringWithString:@""];
5035

51-
NSArray *words = [string componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
36+
NSMutableArray *words = [[string componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] mutableCopy];
5237

38+
//
39+
// Get first letter of the first and last word
40+
//
5341
if ([words count]) {
54-
NSString *firstWord = words[0];
55-
if ([firstWord length]) {
56-
[displayString appendString:[firstWord substringWithRange:NSMakeRange(0, 1)]];
42+
NSString *firstWord = [words firstObject];
43+
if ([firstWord length] != 0) {
44+
[displayString appendString:[firstWord substringToIndex:1]];
5745
}
5846

5947
if ([words count] >= 2) {
60-
NSString *lastWord = words[[words count] - 1];
61-
if ([lastWord length]) {
62-
[displayString appendString:[lastWord substringWithRange:NSMakeRange(0, 1)]];
48+
NSString *lastWord = [words lastObject];
49+
50+
while([lastWord length] == 0 && [words count] >= 2) {
51+
[words removeLastObject];
52+
lastWord = [words lastObject];
53+
}
54+
55+
if([words count] > 1) {
56+
[displayString appendString:[lastWord substringToIndex:1]];
6357
}
6458
}
6559
}
66-
letterLabel.text = [displayString uppercaseString];
6760

68-
//
69-
// Set the background color
70-
//
71-
tempView.backgroundColor = color ? color : [self randomColor];
61+
UIColor *backgroundColor = color ? color : [self randomColor];
7262

73-
//
74-
// Return an image instance of the temporary view
75-
//
76-
self.image = [self imageSnapshotFromView:tempView];
63+
self.image = [self imageSnapshotFromText:[displayString uppercaseString] backgroundColor:backgroundColor];
7764
}
7865

7966
#pragma mark - Helpers
8067

81-
- (UIFont *)fontForLetterLabel {
68+
- (UIFont *)fontForText {
8269
return [UIFont systemFontOfSize:CGRectGetWidth(self.bounds) * 0.48];
8370
}
8471

@@ -102,7 +89,7 @@ - (UIColor *)randomColor {
10289
return [UIColor colorWithRed:red green:green blue:blue alpha:1.0f];
10390
}
10491

105-
- (UIImage *)imageSnapshotFromView:(UIView *)inputView {
92+
- (UIImage *)imageSnapshotFromText:(NSString *)text backgroundColor:(UIColor *)color {
10693

10794
CGFloat scale = [UIScreen mainScreen].scale;
10895

@@ -116,11 +103,24 @@ - (UIImage *)imageSnapshotFromView:(UIView *)inputView {
116103
size.height = floorf(size.height * scale) / scale;
117104
}
118105

119-
UIGraphicsBeginImageContextWithOptions(size, YES, scale);
106+
UIGraphicsBeginImageContextWithOptions(size, NO, scale);
107+
120108
CGContextRef context = UIGraphicsGetCurrentContext();
121-
CGContextTranslateCTM(context, -self.bounds.origin.x, -self.bounds.origin.y);
122109

123-
[inputView.layer renderInContext:context];
110+
//
111+
// Fill background of context
112+
//
113+
CGContextSetFillColorWithColor(context, color.CGColor);
114+
CGContextFillRect(context, CGRectMake(0, 0, size.width, size.height));
115+
116+
//
117+
// Draw text in the context
118+
//
119+
CGSize textSize = [text sizeWithAttributes:@{NSFontAttributeName:[self fontForText]}];
120+
CGRect bounds = self.bounds;
121+
[text drawInRect:CGRectMake(bounds.size.width/2 - textSize.width/2, bounds.size.height/2 - textSize.height/2, textSize.width, textSize.height)
122+
withAttributes:@{NSFontAttributeName:[self fontForText], NSForegroundColorAttributeName:[UIColor whiteColor]}];
123+
124124
UIImage *snapshot = UIGraphicsGetImageFromCurrentImageContext();
125125
UIGraphicsEndImageContext();
126126

0 commit comments

Comments
 (0)