美文网首页糖糖的iOS专题
设置文本框虚线边框

设置文本框虚线边框

作者: 我的梦想之路 | 来源:发表于2016-08-09 11:37 被阅读198次

代码画的

UITextField *entryReason = [[UITextField alloc] init];
    [_popView addSubview:entryReason];
    [entryReason zxp_addConstraints:^(ZXPAutoLayoutMaker *layout) {
        layout.topSpaceByView(defeatReason,11);
        layout.leftSpace(20);
        layout.rightSpace(20);
        layout.heightValue(30);
    }];
    entryReason.borderStyle = UITextBorderStyleNone;
    entryReason.layer.borderWidth = 0;
    entryReason.placeholder = @"申请理由";
    //    设置虚线背景
    entryReason.background = [UIImage imageWithSize:CGSizeMake(250, 30) borderColor:[UIColor grayColor] borderWidth:1];
效果图

UIImage的分类

//添加虚线
+ (UIImage*)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;
}

相关文章

网友评论

    本文标题:设置文本框虚线边框

    本文链接:https://www.haomeiwen.com/subject/hhqdsttx.html