2 bluetooth99 bluetooth99 于 2013.03.07 10:54 提问

给UIView添加虚线边框

怎么样给UIView添加一个虚线边框?

比如像这样:

CSDN移动问答

3个回答

g989_1314125
g989_1314125   2013.03.07 17:04
已采纳

简单:

[yourView.layer setBorderWidth:5.0];
[yourView.layer setBorderColor:[[UIColor colorWithPatternImage:[UIImage imageNamed:@"DotedImage.png"]] CGColor]];///just add image name and create image with dashed or doted drawing and add here

这里只需要添加QuartzCore/QuartzCore.h框架,像下面一样导入.m文件:

#import <QuartzCore/QuartzCore.h>
Mr_me
Mr_me   2013.03.07 11:06

CGContextSetLineDash()方法:

CGFloat dashPattern[]= {3.0, 2};

    context =UIGraphicsGetCurrentContext();
    CGContextSetRGBStrokeColor(context, 1.0, 1.0, 1.0, 1.0);
    // And draw with a blue fill color
    CGContextSetRGBFillColor(context, 0.0, 0.0, 1.0, 1.0);
    // Draw them with a 2.0 stroke width so they are a bit more visible.
    CGContextSetLineWidth(context, 4.0);
    CGContextSetLineDash(context, 0.0, dashPattern, 2);

    CGContextAddRect(context, self.bounds);

    CGContextStrokePath(context);


    // Close the path
    CGContextClosePath(context);
    // Fill & stroke the path
    CGContextDrawPath(context, kCGPathFillStroke);
qq_16665615
qq_16665615   2015.06.04 17:06

该怎么用啊 。。小白求解救

Csdn user default icon
上传中...
上传图片
插入图片