2525#import " UIImageView+Letters.h"
2626
2727// This multiplier sets the font size based on the view bounds
28- static const CGFloat kFontResizingProportion = 0 .48f ;
28+ static const CGFloat kFontResizingProportion = 0 .42f ;
2929
3030@interface UIImageView (LettersPrivate)
3131
32- - (UIImage *)imageSnapshotFromText : (NSString *)text backgroundColor : (UIColor *)color circular : (BOOL )isCircular fontName : ( NSString *) fontName ;
32+ - (UIImage *)imageSnapshotFromText : (NSString *)text backgroundColor : (UIColor *)color circular : (BOOL )isCircular textAttributes : ( NSDictionary *) attributes ;
3333
3434@end
3535
3636@implementation UIImageView (Letters)
3737
3838- (void )setImageWithString : (NSString *)string {
39- [self setImageWithString: string color: nil circular: NO fontName :nil ];
39+ [self setImageWithString: string color: nil circular: NO textAttributes :nil ];
4040}
4141
4242- (void )setImageWithString : (NSString *)string color : (UIColor *)color {
43- [self setImageWithString: string color: color circular: NO fontName :nil ];
43+ [self setImageWithString: string color: color circular: NO textAttributes :nil ];
4444}
4545
4646- (void )setImageWithString : (NSString *)string color : (UIColor *)color circular : (BOOL )isCircular {
47- [self setImageWithString: string color: color circular: isCircular fontName :nil ];
47+ [self setImageWithString: string color: color circular: isCircular textAttributes :nil ];
4848}
4949
50- - (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]];
50+ - (void )setImageWithString : (NSString *)string color : (UIColor *)color circular : (BOOL )isCircular fontName : (NSString *)fontName {
51+ [self setImageWithString: string color: color circular: isCircular textAttributes: @{
52+ NSFontAttributeName :[self fontForFontName: fontName],
53+ NSForegroundColorAttributeName : [UIColor whiteColor ]
54+ }];
5255}
5356
5457- (void )setImageWithString : (NSString *)string color : (UIColor *)color circular : (BOOL )isCircular textAttributes : (NSDictionary *)textAttributes {
58+ if (!textAttributes) {
59+ textAttributes = @{
60+ NSFontAttributeName : [self fontForFontName: nil ],
61+ NSForegroundColorAttributeName : [UIColor whiteColor ]
62+ };
63+ }
64+
5565 NSMutableString *displayString = [NSMutableString stringWithString: @" " ];
5666
5767 NSMutableArray *words = [[string componentsSeparatedByCharactersInSet: [NSCharacterSet whitespaceAndNewlineCharacterSet ]] mutableCopy ];
@@ -61,7 +71,7 @@ - (void)setImageWithString:(NSString *)string color:(UIColor *)color circular:(B
6171 //
6272 if ([words count ]) {
6373 NSString *firstWord = [words firstObject ];
64- if ([firstWord length ] != 0 ) {
74+ if ([firstWord length ]) {
6575 // Get character range to handle emoji (emojis consist of 2 characters in sequence)
6676 NSRange firstLetterRange = [firstWord rangeOfComposedCharacterSequencesForRange: NSMakeRange (0 , 1 )];
6777 [displayString appendString: [firstWord substringWithRange: firstLetterRange]];
@@ -90,7 +100,7 @@ - (void)setImageWithString:(NSString *)string color:(UIColor *)color circular:(B
90100
91101#pragma mark - Helpers
92102
93- - (UIFont *)fontForText : (NSString *)fontName {
103+ - (UIFont *)fontForFontName : (NSString *)fontName {
94104
95105 CGFloat fontSize = CGRectGetWidth (self.bounds ) * kFontResizingProportion ;
96106 if (fontName) {
@@ -122,13 +132,6 @@ - (UIColor *)randomColor {
122132 return [UIColor colorWithRed: red green: green blue: blue alpha: 1 .0f ];
123133}
124134
125- - (NSDictionary *)defaultTextAttributesForFontName : (NSString *)fontName {
126- return @{
127- NSFontAttributeName :[self fontForText: fontName],
128- NSForegroundColorAttributeName :[UIColor whiteColor ]
129- };
130- }
131-
132135- (UIImage *)imageSnapshotFromText : (NSString *)text backgroundColor : (UIColor *)color circular : (BOOL )isCircular textAttributes : (NSDictionary *)textAttributes {
133136
134137 CGFloat scale = [UIScreen mainScreen ].scale ;
0 commit comments