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 ();
0 commit comments