Skip to content

Commit f1b8ecf

Browse files
committed
Added circular param
1 parent 4b0f902 commit f1b8ecf

3 files changed

Lines changed: 32 additions & 3 deletions

File tree

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,16 @@
4242
@param string The string used to generate the initials. This should be a user's full name if available
4343
@param color (optional) This optional paramter sets the background of the image. If not provided, a random color will be generated
4444
*/
45+
4546
- (void)setImageWithString:(NSString *)string color:(UIColor *)color;
4647

48+
/**
49+
Sets the image property of the view based on initial text, a specified background color, and a circular clipping
50+
51+
@param string The string used to generate the initials. This should be a user's full name if available
52+
@param color (optional) This optional paramter sets the background of the image. If not provided, a random color will be generated
53+
@param isCircular This boolean will determine if the image view will be clipped to a circular shape
54+
*/
55+
- (void)setImageWithString:(NSString *)string color:(UIColor *)color circular:(BOOL)isCircular;
56+
4757
@end

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

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,23 @@
2424

2525
#import "UIImageView+Letters.h"
2626

27+
@interface UIImageView (LettersPrivate)
28+
29+
- (UIImage *)imageSnapshotFromText:(NSString *)text backgroundColor:(UIColor *)color circular:(BOOL)isCircular;
30+
31+
@end
32+
2733
@implementation UIImageView (Letters)
2834

2935
- (void)setImageWithString:(NSString *)string {
30-
[self setImageWithString:string color:nil];
36+
[self setImageWithString:string color:nil circular:NO];
3137
}
3238

3339
- (void)setImageWithString:(NSString *)string color:(UIColor *)color {
40+
[self setImageWithString:string color:color circular:NO];
41+
}
42+
43+
- (void)setImageWithString:(NSString *)string color:(UIColor *)color circular:(BOOL)isCircular {
3444
NSMutableString *displayString = [NSMutableString stringWithString:@""];
3545

3646
NSMutableArray *words = [[string componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] mutableCopy];
@@ -60,7 +70,7 @@ - (void)setImageWithString:(NSString *)string color:(UIColor *)color {
6070

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

63-
self.image = [self imageSnapshotFromText:[displayString uppercaseString] backgroundColor:backgroundColor];
73+
self.image = [self imageSnapshotFromText:[displayString uppercaseString] backgroundColor:backgroundColor circular:isCircular];
6474
}
6575

6676
#pragma mark - Helpers
@@ -89,7 +99,7 @@ - (UIColor *)randomColor {
8999
return [UIColor colorWithRed:red green:green blue:blue alpha:1.0f];
90100
}
91101

92-
- (UIImage *)imageSnapshotFromText:(NSString *)text backgroundColor:(UIColor *)color {
102+
- (UIImage *)imageSnapshotFromText:(NSString *)text backgroundColor:(UIColor *)color circular:(BOOL)isCircular {
93103

94104
CGFloat scale = [UIScreen mainScreen].scale;
95105

@@ -121,6 +131,15 @@ - (UIImage *)imageSnapshotFromText:(NSString *)text backgroundColor:(UIColor *)c
121131
[text drawInRect:CGRectMake(bounds.size.width/2 - textSize.width/2, bounds.size.height/2 - textSize.height/2, textSize.width, textSize.height)
122132
withAttributes:@{NSFontAttributeName:[self fontForText], NSForegroundColorAttributeName:[UIColor whiteColor]}];
123133

134+
if (isCircular) {
135+
//
136+
// Clip context to a circle
137+
//
138+
CGPathRef path = CGPathCreateWithEllipseInRect(self.bounds, NULL);
139+
CGContextAddPath(context, path);
140+
CGContextClip(context);
141+
}
142+
124143
UIImage *snapshot = UIGraphicsGetImageFromCurrentImageContext();
125144
UIGraphicsEndImageContext();
126145

0 commit comments

Comments
 (0)