最近做一个项目,在某一些控件上面要求选中的时候是按钮颜色是绿色,未选中的时候按钮背景图是白色且有灰色的边框,由于选中高亮的时候没有边框,而默认状态有边框,因此我在开发过程中这样处理的,自己生成一张带边框的图片和一张不带边框的图片,具体代码如下:
+ (UIImage *)imageWithBorder:(CGFloat)borderW color:(UIColor *)borderColor image:(UIImage *)image cornerRadius:(CGFloat)radius{
//2.开启一个上下文
CGSize size = CGSizeMake(image.size.width + 2 * borderW, image.size.height + 2 * borderW);
UIGraphicsBeginImageContextWithOptions(size, NO, 0);
//3.绘制大矩形,显示出来
//UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 0, size.width, size.height)];
UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(0, 0, size.width, size.height) cornerRadius:radius];
[borderColor set];
[path addClip];
[path fill];//自动将路径添加到上下文
//4.绘制一个小矩形,把小圆设置成裁剪区域
//UIBezierPath *clipPath = [UIBezierPath bezierPathWithRect:CGRectMake(borderW, borderW, image.size.width, image.size.height)];
UIBezierPath *clipPath = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(borderW, borderW, image.size.width, image.size.height) cornerRadius:radius];
[clipPath addClip];//自动将路径添加到上下文,并且超过裁剪区域的路径直接裁减掉,此方法会裁减掉超过大圆的部分
//5.把图片绘制到上下文当中,drawAtPoint画出的图片大小和image大小相同
[image drawAtPoint:CGPointMake(borderW, borderW)];
//6.从上下文当中取出图片
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
//7.关闭上下文
UIGraphicsEndImageContext();
return newImage;
}
/**
根据颜色获取一张图片
@param color 图片的颜色
@return 图片的实例
*/
+ (UIImage *)imageWithColor:(UIColor *)color size:(CGSize)size {
//宽高 1.0只要有值就够了
CGRect rect=CGRectMake(0, 0, size.width, size.height);
UIGraphicsBeginImageContext(rect.size); //在这个范围内开启一段上下文
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [color CGColor]);//在这段上下文中获取到颜色UIColor
CGContextFillRect(context, rect);//用这个颜色填充这个上下文
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();//从这段上下文中获取Image属性,,,结束
UIGraphicsEndImageContext();
return image;
}
调用
UIImage *greenImage=[UIImage imageWithBorder:0.5 color:[UIColor colorWithHexString:@"23b464"] image:[UIImage imageWithColor:[UIColor colorWithHexString:@"23b464"] size:CGSizeMake(90, 30)] cornerRadius:2.0];
[self.closeButton setBackgroundImage:greenImage forState:UIControlStateHighlighted];
UIImage *whiteImae=[UIImage imageWithBorder:0.5 color:[RGB(195, 195, 195)colorWithAlphaComponent:0.7] image:[UIImage imageWithColor:[UIColor whiteColor] size:CGSizeMake(90, 30)] cornerRadius:2.0];
颜色转换的分类
// 颜色转换三:iOS中十六进制的颜色(以#开头)转换为UIColor
+ (UIColor *) colorWithHexString: (NSString *)color{
NSString *cString = [[color stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] uppercaseString];
// String should be 6 or 8 characters
if ([cString length] < 6) {
return [UIColor clearColor];
}
// 判断前缀并剪切掉
if ([cString hasPrefix:@"0X"])
cString = [cString substringFromIndex:2];
if ([cString hasPrefix:@"#"])
cString = [cString substringFromIndex:1];
if ([cString length] != 6)
return [UIColor clearColor];
// 从六位数值中找到RGB对应的位数并转换
NSRange range;
range.location = 0;
range.length = 2;
//R、G、B
NSString *rString = [cString substringWithRange:range];
range.location = 2;
NSString *gString = [cString substringWithRange:range];
range.location = 4;
NSString *bString = [cString substringWithRange:range];
// Scan values
unsigned int r, g, b;
[[NSScanner scannerWithString:rString] scanHexInt:&r];
[[NSScanner scannerWithString:gString] scanHexInt:&g];
[[NSScanner scannerWithString:bString] scanHexInt:&b];
return [UIColor colorWithRed:((float) r / 255.0f) green:((float) g / 255.0f) blue:((float) b / 255.0f) alpha:1.0f];
}
效果图