这是一个易于使用、功能强大的UIImageView分类,它可以生成随机的背景颜色的字母首字母,用作用户头像的占位符。
UIImageView+Letters.{h,m}
文件拖到您的项目中。在您想使用该分类的文件中,务必导入该文件。
#import "UIImageView+Letters.h"
可以在任何UIImageView
实例上调用以下方法来设置图像:
- (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 fontName:(NSString *)fontName
- (void)setImageWithString:(NSString *)string color:(UIColor *)color circular:(BOOL)isCircular textAttributes:(NSDictionary *)textAttributes
string
用于生成首字母的字符串。如果可能,应为用户的完整姓名。
color
是一个可选参数,用于设置图像的背景颜色。传入nil
将自动为您生成颜色。
isCircular
是一个布尔参数,如果启用,将自动裁剪图像成圆形。
fontName
是一个指定自定义字体的字符串。传入nil
将默认使用系统字体。提供的字体标识符列表可以在此处找到。
textAttributes
是一个NSDictionary,允许您使用在NSAttributedString.h
中找到的键为字母的文本指定字体、文本颜色、阴影属性等。
NSString *userName = @"Michael Bluth";
UIImageView *myImgView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 50, 50)];
[myImgView setImageWithString:userName color:nil circular:YES];
如果您喜欢这个工具,请通过下载免费的Reach Contact List应用程序来支持它!
使用MIT许可证。有关详细信息,请参阅许可证文件。