最近做一個項目骂铁,在某一些控件上面要求選中的時候是按鈕顏色是綠色,未選中的時候按鈕背景圖是白色且有灰色的邊框漫雕,由于選中高亮的時候沒有邊框滨嘱,而默認(rèn)狀態(tài)有邊框,因此我在開發(fā)過程中這樣處理的浸间,自己生成一張帶邊框的圖片和一張不帶邊框的圖片太雨,具體代碼如下:
+ (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.繪制一個小矩形,把小圓設(shè)置成裁剪區(qū)域
//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];//自動將路徑添加到上下文,并且超過裁剪區(qū)域的路徑直接裁減掉魁蒜,此方法會裁減掉超過大圓的部分
//5.把圖片繪制到上下文當(dāng)中,drawAtPoint畫出的圖片大小和image大小相同
[image drawAtPoint:CGPointMake(borderW, borderW)];
//6.從上下文當(dāng)中取出圖片
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
//7.關(guān)閉上下文
UIGraphicsEndImageContext();
return newImage;
}
/**
根據(jù)顏色獲取一張圖片
@param color 圖片的顏色
@return 圖片的實例
*/
+ (UIImage *)imageWithColor:(UIColor *)color size:(CGSize)size {
//寬高 1.0只要有值就夠了
CGRect rect=CGRectMake(0, 0, size.width, size.height);
UIGraphicsBeginImageContext(rect.size); //在這個范圍內(nèi)開啟一段上下文
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [color CGColor]);//在這段上下文中獲取到顏色UIColor
CGContextFillRect(context, rect);//用這個顏色填充這個上下文
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();//從這段上下文中獲取Image屬性,,,結(jié)束
UIGraphicsEndImageContext();
return image;
}
調(diào)用
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];
顏色轉(zhuǎn)換的分類
// 顏色轉(zhuǎn)換三:iOS中十六進制的顏色(以#開頭)轉(zhuǎn)換為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];
// 從六位數(shù)值中找到RGB對應(yīng)的位數(shù)并轉(zhuǎn)換
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];
}
效果圖
效果圖.png