UIImageView-Letters
https://github.com/bachonk/UIImageView-Letters
An easy, helpful UIImageView category that generates letter initials as a placeholder for user profile images, with a randomized background color
一个简单,易用的,根据用户的英文名字生成了一个placeholder文件,并随机赋上一种颜色
Usage
In the file where you want to use the category, be sure to import the file.
#import "UIImageView+Letters.h"
在你想用的地方,引入头文件即可
Call the following methods on any UIImageView
instance to set the image:
你可以给UIImageView调用如下两种方法:
- (void)setImageWithString:(NSString *)string
- (void)setImageWithString:(NSString *)string color:(UIColor *)color
string
is the string used to generate the initials. This should be a user's full name if available.
string是用来产生缩写的,当然,这个得需要是用户合法的全名。
color
is an optional parameter that sets the background color of the image. Pass in nil
to have a color automatically generated for you.
颜色是备选参数,用来设置图片背景色的。如果你传递了nil值,他会给你随机产生一种颜色。
NSString *userName = @"Michael Bluth";
UIImageView *myImgView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 50, 50)];
[myImgView setImageWithString:userName];
以下附上本人的使用教程:
// // RootViewController.m // Letter // // Created by YouXianMing on 14-9-13. // Copyright (c) 2014年 YouXianMing. All rights reserved. // #import "RootViewController.h" #import "UIImageView+Letters.h" @interface RootViewController () @end @implementation RootViewController - (void)viewDidLoad { [super viewDidLoad]; NSArray *nameArray = @[@"Michael Bluth", @"You Xian", @"Jim Simith", @"Li Dian", @"Jun Gang", @"You Jin", @"Lin Ken", @"Li kui", @"Leter Jun"]; for (int i = 0; i < nameArray.count; i++) { NSString *userName = nameArray[i]; UIImageView *myImgView = \ [[UIImageView alloc] initWithFrame:CGRectMake(30 + (i%3)*100, // 求余 30 + (i/3)*100, // 取整 60, 60)]; myImgView.layer.cornerRadius = 30.f; myImgView.layer.masksToBounds = YES; [myImgView setImageWithString:userName]; [self.view addSubview:myImgView]; } } @end
再来看看人家的源码: