Skip to content

Commit 4cbebfa

Browse files
committed
Fixed private methods; updated sample project
1 parent f8a531e commit 4cbebfa

3 files changed

Lines changed: 37 additions & 17 deletions

File tree

UIImageView+Letters/UIImageView+Letters.m

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -25,33 +25,43 @@
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;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<Bucket
3+
type = "1"
4+
version = "2.0">
5+
<Breakpoints>
6+
<BreakpointProxy
7+
BreakpointExtensionID = "Xcode.Breakpoint.ExceptionBreakpoint">
8+
<BreakpointContent
9+
shouldBeEnabled = "Yes"
10+
ignoreCount = "0"
11+
continueAfterRunningActions = "No"
12+
scope = "0"
13+
stopOnStyle = "0">
14+
</BreakpointContent>
15+
</BreakpointProxy>
16+
</Breakpoints>
17+
</Bucket>

UIImageViewLettersSample/UIImageViewLettersSample/ViewController.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ - (void)didReceiveMemoryWarning {
3939
- (IBAction)refreshSampleImage:(id)sender {
4040

4141
if (_useFuturaSwitch.isOn) {
42-
[_sampleImageView setImageWithString:_nameField.text color:nil circular:_circularSwitch.isOn fontName:@"Futura-Medium"];
42+
[_sampleImageView setImageWithString:_nameField.text color:nil circular:_circularSwitch.isOn textAttributes:@{NSFontAttributeName:[UIFont fontWithName:@"Futura-Medium" size:28.0f], NSForegroundColorAttributeName:[UIColor blueColor]}];
4343
} else {
4444
[_sampleImageView setImageWithString:_nameField.text color:nil circular:_circularSwitch.isOn];
4545
}

0 commit comments

Comments
 (0)