diff --git a/README.md b/README.md index f3747fe..d0aa308 100644 --- a/README.md +++ b/README.md @@ -33,6 +33,7 @@ Call the following methods on any `UIImageView` instance to set the image: + `- (void)setImageWithString:(NSString *)string` + `- (void)setImageWithString:(NSString *)string color:(UIColor *)color` + `- (void)setImageWithString:(NSString *)string color:(UIColor *)color circular:(BOOL)isCircular` ++ `- (void)setImageWithString:(NSString *)string color:(UIColor *)color circular:(BOOL)isCircular fontWithName: (NSString *) fontName` `string` is the string used to generate the initials. This should be a user's full name if available. @@ -40,6 +41,8 @@ Call the following methods on any `UIImageView` instance to set the image: `isCircular` is a boolean parameter that will automatically clip the image to a circle if enabled. +`fontName` is a NSString that specifies a custom font. See all String Identifiers for built in iOS fonts [here](http://iosfonts.com). + ##### Example ``` diff --git a/UIImageView+Letters/UIImageView+Letters.h b/UIImageView+Letters/UIImageView+Letters.h index 871f281..c3bc936 100644 --- a/UIImageView+Letters/UIImageView+Letters.h +++ b/UIImageView+Letters/UIImageView+Letters.h @@ -54,4 +54,14 @@ */ - (void)setImageWithString:(NSString *)string color:(UIColor *)color circular:(BOOL)isCircular; +/** + Sets the image property of the view based on initial text, a specified background color, a custom font, and a circular clipping + + @param string The string used to generate the initials. This should be a user's full name if available + @param color (optional) This optional paramter sets the background of the image. If not provided, a random color will be generated + @param isCircular This boolean will determine if the image view will be clipped to a circular shape + @param fontName will use a custom font rather than System Font + */ +- (void)setImageWithString:(NSString *)string color:(UIColor *)color circular:(BOOL)isCircular fontWithName: (NSString *) fontName; + @end diff --git a/UIImageView+Letters/UIImageView+Letters.m b/UIImageView+Letters/UIImageView+Letters.m index abc6ed2..0a6cbb9 100644 --- a/UIImageView+Letters/UIImageView+Letters.m +++ b/UIImageView+Letters/UIImageView+Letters.m @@ -26,7 +26,7 @@ @interface UIImageView (LettersPrivate) -- (UIImage *)imageSnapshotFromText:(NSString *)text backgroundColor:(UIColor *)color circular:(BOOL)isCircular; +- (UIImage *)imageSnapshotFromText:(NSString *)text backgroundColor:(UIColor *)color circular:(BOOL)isCircular fontName: (NSString*) fontName; @end @@ -41,6 +41,10 @@ - (void)setImageWithString:(NSString *)string color:(UIColor *)color { } - (void)setImageWithString:(NSString *)string color:(UIColor *)color circular:(BOOL)isCircular { + [self setImageWithString:string color:color circular:isCircular fontWithName:nil]; +} + +- (void)setImageWithString:(NSString *)string color:(UIColor *)color circular:(BOOL)isCircular fontWithName: (NSString *) fontName { NSMutableString *displayString = [NSMutableString stringWithString:@""]; NSMutableArray *words = [[string componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] mutableCopy]; @@ -70,13 +74,19 @@ - (void)setImageWithString:(NSString *)string color:(UIColor *)color circular:(B UIColor *backgroundColor = color ? color : [self randomColor]; - self.image = [self imageSnapshotFromText:[displayString uppercaseString] backgroundColor:backgroundColor circular:isCircular]; + self.image = [self imageSnapshotFromText:[displayString uppercaseString] backgroundColor:backgroundColor circular:isCircular fontName:fontName]; } #pragma mark - Helpers -- (UIFont *)fontForText { - return [UIFont systemFontOfSize:CGRectGetWidth(self.bounds) * 0.48]; +- (UIFont *)fontForText: (NSString *) fontName { + + if (fontName) { + return [UIFont fontWithName:fontName size:CGRectGetWidth(self.bounds) * 0.48]; + } else { + return [UIFont systemFontOfSize:CGRectGetWidth(self.bounds) * 0.48]; + } + } - (UIColor *)randomColor { @@ -99,7 +109,7 @@ - (UIColor *)randomColor { return [UIColor colorWithRed:red green:green blue:blue alpha:1.0f]; } -- (UIImage *)imageSnapshotFromText:(NSString *)text backgroundColor:(UIColor *)color circular:(BOOL)isCircular { +- (UIImage *)imageSnapshotFromText:(NSString *)text backgroundColor:(UIColor *)color circular:(BOOL)isCircular fontName:(NSString *)fontName{ CGFloat scale = [UIScreen mainScreen].scale; @@ -136,10 +146,10 @@ - (UIImage *)imageSnapshotFromText:(NSString *)text backgroundColor:(UIColor *)c // // Draw text in the context // - CGSize textSize = [text sizeWithAttributes:@{NSFontAttributeName:[self fontForText]}]; + CGSize textSize = [text sizeWithAttributes:@{NSFontAttributeName:[self fontForText: fontName]}]; CGRect bounds = self.bounds; [text drawInRect:CGRectMake(bounds.size.width/2 - textSize.width/2, bounds.size.height/2 - textSize.height/2, textSize.width, textSize.height) - withAttributes:@{NSFontAttributeName:[self fontForText], NSForegroundColorAttributeName:[UIColor whiteColor]}]; + withAttributes:@{NSFontAttributeName:[self fontForText: fontName], NSForegroundColorAttributeName:[UIColor whiteColor]}]; UIImage *snapshot = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); diff --git a/UIImageViewLettersSample/UIImageViewLettersSample.xcodeproj/project.xcworkspace/xcshareddata/UIImageViewLettersSample.xccheckout b/UIImageViewLettersSample/UIImageViewLettersSample.xcodeproj/project.xcworkspace/xcshareddata/UIImageViewLettersSample.xccheckout new file mode 100644 index 0000000..647f648 --- /dev/null +++ b/UIImageViewLettersSample/UIImageViewLettersSample.xcodeproj/project.xcworkspace/xcshareddata/UIImageViewLettersSample.xccheckout @@ -0,0 +1,41 @@ + + + + + IDESourceControlProjectFavoriteDictionaryKey + + IDESourceControlProjectIdentifier + A3F1ED25-0928-4526-9FF8-C73C2AFAA60A + IDESourceControlProjectName + UIImageViewLettersSample + IDESourceControlProjectOriginsDictionary + + 9FFD21C6A65AE22198CFA6E939DC569037B4263C + github.com:MattFaluotico/UIImageView-Letters.git + + IDESourceControlProjectPath + UIImageViewLettersSample/UIImageViewLettersSample.xcodeproj + IDESourceControlProjectRelativeInstallPathDictionary + + 9FFD21C6A65AE22198CFA6E939DC569037B4263C + ../../.. + + IDESourceControlProjectURL + github.com:MattFaluotico/UIImageView-Letters.git + IDESourceControlProjectVersion + 111 + IDESourceControlProjectWCCIdentifier + 9FFD21C6A65AE22198CFA6E939DC569037B4263C + IDESourceControlProjectWCConfigurations + + + IDESourceControlRepositoryExtensionIdentifierKey + public.vcs.git + IDESourceControlWCCIdentifierKey + 9FFD21C6A65AE22198CFA6E939DC569037B4263C + IDESourceControlWCCName + UIImageView-Letters + + + + diff --git a/UIImageViewLettersSample/UIImageViewLettersSample.xcodeproj/xcuserdata/mf.xcuserdatad/xcschemes/UIImageViewLettersSample.xcscheme b/UIImageViewLettersSample/UIImageViewLettersSample.xcodeproj/xcuserdata/mf.xcuserdatad/xcschemes/UIImageViewLettersSample.xcscheme new file mode 100644 index 0000000..b01d951 --- /dev/null +++ b/UIImageViewLettersSample/UIImageViewLettersSample.xcodeproj/xcuserdata/mf.xcuserdatad/xcschemes/UIImageViewLettersSample.xcscheme @@ -0,0 +1,112 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/UIImageViewLettersSample/UIImageViewLettersSample.xcodeproj/xcuserdata/mf.xcuserdatad/xcschemes/xcschememanagement.plist b/UIImageViewLettersSample/UIImageViewLettersSample.xcodeproj/xcuserdata/mf.xcuserdatad/xcschemes/xcschememanagement.plist new file mode 100644 index 0000000..5617c28 --- /dev/null +++ b/UIImageViewLettersSample/UIImageViewLettersSample.xcodeproj/xcuserdata/mf.xcuserdatad/xcschemes/xcschememanagement.plist @@ -0,0 +1,27 @@ + + + + + SchemeUserState + + UIImageViewLettersSample.xcscheme + + orderHint + 0 + + + SuppressBuildableAutocreation + + C855D2E01A00593300A5B693 + + primary + + + C855D2F91A00593300A5B693 + + primary + + + + + diff --git a/UIImageViewLettersSample/UIImageViewLettersSample/Base.lproj/Main.storyboard b/UIImageViewLettersSample/UIImageViewLettersSample/Base.lproj/Main.storyboard index 664ef03..0316dbc 100644 --- a/UIImageViewLettersSample/UIImageViewLettersSample/Base.lproj/Main.storyboard +++ b/UIImageViewLettersSample/UIImageViewLettersSample/Base.lproj/Main.storyboard @@ -1,7 +1,7 @@ - + - + @@ -16,51 +16,87 @@ - + + + + - - + + + + - - - - - - - - + + + + + + + + + + + + + + + + + + + diff --git a/UIImageViewLettersSample/UIImageViewLettersSample/UIImageView+Letters/UIImageView+Letters.h b/UIImageViewLettersSample/UIImageViewLettersSample/UIImageView+Letters/UIImageView+Letters.h index 871f281..c3bc936 100644 --- a/UIImageViewLettersSample/UIImageViewLettersSample/UIImageView+Letters/UIImageView+Letters.h +++ b/UIImageViewLettersSample/UIImageViewLettersSample/UIImageView+Letters/UIImageView+Letters.h @@ -54,4 +54,14 @@ */ - (void)setImageWithString:(NSString *)string color:(UIColor *)color circular:(BOOL)isCircular; +/** + Sets the image property of the view based on initial text, a specified background color, a custom font, and a circular clipping + + @param string The string used to generate the initials. This should be a user's full name if available + @param color (optional) This optional paramter sets the background of the image. If not provided, a random color will be generated + @param isCircular This boolean will determine if the image view will be clipped to a circular shape + @param fontName will use a custom font rather than System Font + */ +- (void)setImageWithString:(NSString *)string color:(UIColor *)color circular:(BOOL)isCircular fontWithName: (NSString *) fontName; + @end diff --git a/UIImageViewLettersSample/UIImageViewLettersSample/UIImageView+Letters/UIImageView+Letters.m b/UIImageViewLettersSample/UIImageViewLettersSample/UIImageView+Letters/UIImageView+Letters.m index 29a9e9c..0a6cbb9 100644 --- a/UIImageViewLettersSample/UIImageViewLettersSample/UIImageView+Letters/UIImageView+Letters.m +++ b/UIImageViewLettersSample/UIImageViewLettersSample/UIImageView+Letters/UIImageView+Letters.m @@ -26,7 +26,7 @@ @interface UIImageView (LettersPrivate) -- (UIImage *)imageSnapshotFromText:(NSString *)text backgroundColor:(UIColor *)color circular:(BOOL)isCircular; +- (UIImage *)imageSnapshotFromText:(NSString *)text backgroundColor:(UIColor *)color circular:(BOOL)isCircular fontName: (NSString*) fontName; @end @@ -41,6 +41,10 @@ - (void)setImageWithString:(NSString *)string color:(UIColor *)color { } - (void)setImageWithString:(NSString *)string color:(UIColor *)color circular:(BOOL)isCircular { + [self setImageWithString:string color:color circular:isCircular fontWithName:nil]; +} + +- (void)setImageWithString:(NSString *)string color:(UIColor *)color circular:(BOOL)isCircular fontWithName: (NSString *) fontName { NSMutableString *displayString = [NSMutableString stringWithString:@""]; NSMutableArray *words = [[string componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] mutableCopy]; @@ -63,20 +67,26 @@ - (void)setImageWithString:(NSString *)string color:(UIColor *)color circular:(B } if([words count] > 1) { - [displayString appendString:[lastWord substringToIndex:1]]; + [displayString appendString:[lastWord substringToIndex:1]]; } } } UIColor *backgroundColor = color ? color : [self randomColor]; - self.image = [self imageSnapshotFromText:[displayString uppercaseString] backgroundColor:backgroundColor circular:isCircular]; + self.image = [self imageSnapshotFromText:[displayString uppercaseString] backgroundColor:backgroundColor circular:isCircular fontName:fontName]; } #pragma mark - Helpers -- (UIFont *)fontForText { - return [UIFont systemFontOfSize:CGRectGetWidth(self.bounds) * 0.48]; +- (UIFont *)fontForText: (NSString *) fontName { + + if (fontName) { + return [UIFont fontWithName:fontName size:CGRectGetWidth(self.bounds) * 0.48]; + } else { + return [UIFont systemFontOfSize:CGRectGetWidth(self.bounds) * 0.48]; + } + } - (UIColor *)randomColor { @@ -99,7 +109,7 @@ - (UIColor *)randomColor { return [UIColor colorWithRed:red green:green blue:blue alpha:1.0f]; } -- (UIImage *)imageSnapshotFromText:(NSString *)text backgroundColor:(UIColor *)color circular:(BOOL)isCircular { +- (UIImage *)imageSnapshotFromText:(NSString *)text backgroundColor:(UIColor *)color circular:(BOOL)isCircular fontName:(NSString *)fontName{ CGFloat scale = [UIScreen mainScreen].scale; @@ -136,10 +146,10 @@ - (UIImage *)imageSnapshotFromText:(NSString *)text backgroundColor:(UIColor *)c // // Draw text in the context // - CGSize textSize = [text sizeWithAttributes:@{NSFontAttributeName:[self fontForText]}]; + CGSize textSize = [text sizeWithAttributes:@{NSFontAttributeName:[self fontForText: fontName]}]; CGRect bounds = self.bounds; [text drawInRect:CGRectMake(bounds.size.width/2 - textSize.width/2, bounds.size.height/2 - textSize.height/2, textSize.width, textSize.height) - withAttributes:@{NSFontAttributeName:[self fontForText], NSForegroundColorAttributeName:[UIColor whiteColor]}]; + withAttributes:@{NSFontAttributeName:[self fontForText: fontName], NSForegroundColorAttributeName:[UIColor whiteColor]}]; UIImage *snapshot = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); diff --git a/UIImageViewLettersSample/UIImageViewLettersSample/ViewController.h b/UIImageViewLettersSample/UIImageViewLettersSample/ViewController.h index 1af4ebe..14df16e 100644 --- a/UIImageViewLettersSample/UIImageViewLettersSample/ViewController.h +++ b/UIImageViewLettersSample/UIImageViewLettersSample/ViewController.h @@ -17,6 +17,8 @@ @property (nonatomic, weak) IBOutlet UISwitch *circularSwitch; +@property (weak, nonatomic) IBOutlet UISwitch *useFuturaSwitch; + - (IBAction)refreshSampleImage:(id)sender; @end diff --git a/UIImageViewLettersSample/UIImageViewLettersSample/ViewController.m b/UIImageViewLettersSample/UIImageViewLettersSample/ViewController.m index 73f649a..daf99c1 100644 --- a/UIImageViewLettersSample/UIImageViewLettersSample/ViewController.m +++ b/UIImageViewLettersSample/UIImageViewLettersSample/ViewController.m @@ -38,7 +38,14 @@ - (void)didReceiveMemoryWarning { - (IBAction)refreshSampleImage:(id)sender { - [_sampleImageView setImageWithString:_nameField.text color:nil circular:_circularSwitch.isOn]; + if (_useFuturaSwitch.isOn) { + [_sampleImageView setImageWithString:_nameField.text color:nil circular:_circularSwitch.isOn fontWithName:@"Futura-Medium"]; + } else { + [_sampleImageView setImageWithString:_nameField.text color:nil circular:_circularSwitch.isOn]; + } + + + }