虚线边框的实现

1.创建类继承UIImage;

2.创建绘制虚线的方法实现:

+ (id)imageWithSize:(CGSize)size borderColor:(UIColor *)color borderWidth:(CGFloat)borderWidth
{
    //开启图片上下文
    UIGraphicsBeginImageContextWithOptions(size, NO, 0.0);
    //设置颜色
    [[UIColor clearColor] set];
    //取得当前上下文
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextBeginPath(context);
    //设置属性
    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();
    //关闭图片上下文
    UIGraphicsEndImageContext();
    
    return image;
}

 

3.外部调用.  记住 :要用类方法调用(好粗心.....一开始做居然没用类方法调用,导致一直卡在方法不能调的情况....)

 

- (void)viewDidLoad {
    [super viewDidLoad];
   
    //实现虚线边框  类方法调用
    
    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];
    
}

我也是根据别人的博客练习来做的,学习学习..呵呵.....

转载于:https://www.cnblogs.com/pengsi/p/4844599.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值