虚线边框的实现
1.創(chuàng)建類繼承UIImage;
2.創(chuàng)建繪制虛線的方法實(shí)現(xiàn):
+ (id)imageWithSize:(CGSize)size borderColor:(UIColor *)color borderWidth:(CGFloat)borderWidth
{
??? //開(kāi)啟圖片上下文
??? UIGraphicsBeginImageContextWithOptions(size, NO, 0.0);
??? //設(shè)置顏色
??? [[UIColor clearColor] set];
??? //取得當(dāng)前上下文
??? CGContextRef context = UIGraphicsGetCurrentContext();
??? CGContextBeginPath(context);
??? //設(shè)置屬性
??? CGContextSetLineWidth(context, borderWidth);
??? CGContextSetStrokeColorWithColor(context, color.CGColor);
??? CGFloat lengths[] = { 3, 1 };
??? CGContextSetLineDash(context, 0, lengths, 1);
??? CGContextMoveToPoint(context, 0.0, 0.0);
??? CGContextAddLineToPoint(context, size.width, 0.0);
??? CGContextAddLineToPoint(context, size.width, size.height);
??? CGContextAddLineToPoint(context, 0, size.height);
??? CGContextAddLineToPoint(context, 0.0, 0.0);
??? //繪制
??? CGContextStrokePath(context);
??? //取得繪制的圖片的上下文
??? UIImage* image = UIGraphicsGetImageFromCurrentImageContext();
??? //關(guān)閉圖片上下文
??? UIGraphicsEndImageContext();
?? ?
??? return image;
}
?
3.外部調(diào)用.? 記住 :要用類方法調(diào)用(好粗心.....一開(kāi)始做居然沒(méi)用類方法調(diào)用,導(dǎo)致一直卡在方法不能調(diào)的情況....)
?
- (void)viewDidLoad {
??? [super viewDidLoad];
? ?
??? //實(shí)現(xiàn)虛線邊框? 類方法調(diào)用
?? ?
??? UIImage *img = [CreatImage imageWithSize:CGSizeMake(200, 200) borderColor:[UIColor redColor] borderWidth:3];
?? ?
??? UIImageView *img1 = [[UIImageView alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];
??? [img1 setImage:img];
??? [self.view addSubview:img1];
?? ?
}
我也是根據(jù)別人的博客練習(xí)來(lái)做的,學(xué)習(xí)學(xué)習(xí)..呵呵.....
轉(zhuǎn)載于:https://www.cnblogs.com/pengsi/p/4844599.html
總結(jié)
- 上一篇: Oracle 哈希连接原理
- 下一篇: CSS-3 Animation 的使用