Skip to content

Commit d15a5a5

Browse files
committed
Add option for NSAttributedString attributes on letters
1 parent 7f362ee commit d15a5a5

3 files changed

Lines changed: 27 additions & 7 deletions

File tree

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ Call the following methods on any `UIImageView` instance to set the image:
3434
+ `- (void)setImageWithString:(NSString *)string color:(UIColor *)color`
3535
+ `- (void)setImageWithString:(NSString *)string color:(UIColor *)color circular:(BOOL)isCircular`
3636
+ `- (void)setImageWithString:(NSString *)string color:(UIColor *)color circular:(BOOL)isCircular fontName:(NSString *)fontName`
37+
+ `- (void)setImageWithString:(NSString *)string color:(UIColor *)color circular:(BOOL)isCircular textAttributes:(NSDictionary *)textAttributes`
3738

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

@@ -43,6 +44,8 @@ Call the following methods on any `UIImageView` instance to set the image:
4344

4445
`fontName` is a string that specifies a custom font. Pass in `nil` to use the system font by default. The list of provided font identifiers can be found [here](http://iosfonts.com).
4546

47+
`textAttributes` is an NSDictionary that allows you to specify font, text color, shadow properties, etc., for the letters text, using the keys found in `NSAttributedString.h`.
48+
4649
##### Example
4750

4851
```

UIImageView+Letters/UIImageView+Letters.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,4 +64,14 @@
6464
*/
6565
- (void)setImageWithString:(NSString *)string color:(UIColor *)color circular:(BOOL)isCircular fontName:(NSString *)fontName;
6666

67+
/**
68+
Sets the image property of the view based on initial text, a specified background color, custom text attributes, and a circular clipping
69+
70+
@param string The string used to generate the initials. This should be a user's full name if available
71+
@param color (optional) This optional paramter sets the background of the image. If not provided, a random color will be generated
72+
@param isCircular This boolean will determine if the image view will be clipped to a circular shape
73+
@param textAttributes This dictionary allows you to specify font, text color, shadow properties, etc., for the letters text, using the keys found in NSAttributedString.h
74+
*/
75+
- (void)setImageWithString:(NSString *)string color:(UIColor *)color circular:(BOOL)isCircular textAttributes:(NSDictionary *)textAttributes;
76+
6777
@end

UIImageView+Letters/UIImageView+Letters.m

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@ - (void)setImageWithString:(NSString *)string color:(UIColor *)color circular:(B
4848
}
4949

5050
- (void)setImageWithString:(NSString *)string color:(UIColor *)color circular:(BOOL)isCircular fontName: (NSString *)fontName {
51+
[self setImageWithString:string color:color circular:isCircular textAttributes:[self defaultTextAttributesForFontName:fontName]];
52+
}
53+
54+
- (void)setImageWithString:(NSString *)string color:(UIColor *)color circular:(BOOL)isCircular textAttributes:(NSDictionary *)textAttributes {
5155
NSMutableString *displayString = [NSMutableString stringWithString:@""];
5256

5357
NSMutableArray *words = [[string componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] mutableCopy];
@@ -80,8 +84,8 @@ - (void)setImageWithString:(NSString *)string color:(UIColor *)color circular:(B
8084
}
8185

8286
UIColor *backgroundColor = color ? color : [self randomColor];
83-
84-
self.image = [self imageSnapshotFromText:[displayString uppercaseString] backgroundColor:backgroundColor circular:isCircular fontName:fontName];
87+
88+
self.image = [self imageSnapshotFromText:[displayString uppercaseString] backgroundColor:backgroundColor circular:isCircular textAttributes:textAttributes];
8589
}
8690

8791
#pragma mark - Helpers
@@ -118,7 +122,14 @@ - (UIColor *)randomColor {
118122
return [UIColor colorWithRed:red green:green blue:blue alpha:1.0f];
119123
}
120124

121-
- (UIImage *)imageSnapshotFromText:(NSString *)text backgroundColor:(UIColor *)color circular:(BOOL)isCircular fontName:(NSString *)fontName {
125+
- (NSDictionary *)defaultTextAttributesForFontName:(NSString *)fontName {
126+
return @{
127+
NSFontAttributeName:[self fontForText:fontName],
128+
NSForegroundColorAttributeName:[UIColor whiteColor]
129+
};
130+
}
131+
132+
- (UIImage *)imageSnapshotFromText:(NSString *)text backgroundColor:(UIColor *)color circular:(BOOL)isCircular textAttributes:(NSDictionary *)textAttributes {
122133

123134
CGFloat scale = [UIScreen mainScreen].scale;
124135

@@ -155,10 +166,6 @@ - (UIImage *)imageSnapshotFromText:(NSString *)text backgroundColor:(UIColor *)c
155166
//
156167
// Draw text in the context
157168
//
158-
NSDictionary *textAttributes = @{
159-
NSFontAttributeName:[self fontForText:fontName],
160-
NSForegroundColorAttributeName:[UIColor whiteColor]
161-
};
162169
CGSize textSize = [text sizeWithAttributes:textAttributes];
163170
CGRect bounds = self.bounds;
164171

0 commit comments

Comments
 (0)