zoukankan      html  css  js  c++  java
  • iOS给一个view添加虚线边框

    /**

     * 此方法作用:给一个矩形视图增加四条矩形虚线边框

     *

     * parma:superView:需要加载的父视图

     */

    #define padding 20

     

    - (void)addDottedLineFromImageView:(UIView *)superView{

        CGFloat w = superView.frame.size.width;

        CGFloat h = superView.frame.size.height;

        

    //创建四个imageView作边框

        for (NSInteger i = 0; i<4; i++) {

            UIImageView *imageView = [[UIImageView alloc] init];

       imageView.backgroundColor = [UIColor clearColor];

            if (i == 0) {

                imageView.frame = CGRectMake(0, 0, w, padding);

            }else if (i == 1){

                imageView.frame = CGRectMake(0, 0, padding, h);

            }else if (i == 2){

                imageView.frame = CGRectMake(0, h - padding, w, padding);

            }else if (i == 3){

                imageView.frame = CGRectMake(w - padding, 0, padding, h);

            }

            [superView addSubview:imageView];

            

            UIGraphicsBeginImageContext(imageView.frame.size);   //开始画线

            [imageView.image drawInRect:CGRectMake(0, 0, imageView.frame.size.width, imageView.frame.size.height)];

            CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);  //设置线条终点形状

            

            CGFloat lengths[] = {10,5};

            CGContextRef line = UIGraphicsGetCurrentContext();

            CGContextSetStrokeColorWithColor(line, [UIColor blackColor].CGColor);

            CGContextSetLineDash(line, 0, lengths, 2);  //画虚线 

            CGContextMoveToPoint(line, 0, 0);    //开始画线

            

            if (i == 0) {

                CGContextAddLineToPoint(line, w - padding, 0);

            }else if (i == 1){

                CGContextAddLineToPoint(line, 0, w);

            }else if (i == 2){

                CGContextMoveToPoint(line, 0, padding);

                CGContextAddLineToPoint(line, w, padding);

            }else if (i == 3){

                CGContextMoveToPoint(line, padding, 0);

                CGContextAddLineToPoint(line, padding, w);

            }

            

            CGContextStrokePath(line);

            imageView.image = UIGraphicsGetImageFromCurrentImageContext();

        }

    }

  • 相关阅读:
    转:关于国外硕博士论文搜索和下载的讨论
    转:如何查找别人论文(计算机类文献)中实验的代码?
    jQuery基础知识二
    jQuery基础知识笔记一
    jQuery基础知识一
    JS知识回顾
    JS的DOM(获取元素、元素属性、value属性、显示时间、计时器、节点增删改查等)
    JS基础知识三(正则表达式、arguments变量、JS事件、onsubmit事件、各种对象)
    JS基础知识小结二
    JS基础知识二(函数、全局/局部变量、对象、方法)
  • 原文地址:https://www.cnblogs.com/chenjie-ios/p/4709818.html
Copyright © 2011-2022 走看看