利用Quartz2D设置自定义条纹背景

1、设置平铺背景

   self.view.backgroundColor = [UIColor colorWithPatternImage:newImage];

2、设置拉伸背景

UIImage *oldImage = [UIImage imageNamed:@"me"];
    
    UIGraphicsBeginImageContextWithOptions(self.view.frame.size, NO, 0.0);
    [oldImage drawInRect:self.view.bounds];
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    self.view.backgroundColor = [UIColor colorWithPatternImage:newImage];

3、设置条纹背景 

//view :传入VIEW viewBgColor:View背景色 rowHeight:条纹高度
//lineWidth :底端线条粗细  margin:距离传入VIEW左右间隙
-(void)setBgStyleWithView:(UIView *)view viewBgColor:(UIColor *)bgColor  rowHeight:(CGFloat)rowHeight lineWidth:(CGFloat )lineWidth margin:(CGFloat )margin{
    //可以改造 把view 设置为属性  self.yourview  替换掉 view
    //背景色
    view.backgroundColor = bgColor;
    // 1.创建一行背景图片
    CGFloat rowW = self.view.frame.size.width;
    CGFloat rowH = rowHeight;
    UIGraphicsBeginImageContextWithOptions(CGSizeMake(rowW, rowH), NO, 0.0);
    
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    // 画矩形框
    [[UIColor redColor] set];
    CGContextAddRect(ctx, CGRectMake(0, 0, rowW, rowH));
    CGContextFillPath(ctx);
    
    // 2.画线
    [[UIColor greenColor] set];
    CGContextSetLineWidth(ctx, lineWidth);
    CGFloat dividerX = margin;
    CGFloat dividerY = rowH - lineWidth;
    CGContextMoveToPoint(ctx, dividerX, dividerY);
    CGContextAddLineToPoint(ctx, rowW - dividerX, dividerY);
    CGContextStrokePath(ctx);
    // 3.取图
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    // 4.结束上下文
    UIGraphicsEndImageContext();
    // 5.设置为背景
 view.backgroundColor = [UIColor colorWithPatternImage:newImage];
}

4、例图






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值