设置文本框虚线边框

代码画的

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;
}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,281评论 4 61
  • Swift版本点击这里欢迎加入QQ群交流: 594119878最新更新日期:18-09-17 About A cu...
    ylgwhyh阅读 25,634评论 7 249
  • l 路遇一只虫...特别漂亮的虫... 如果它很娘炮: -咦...你为什么要拍人家...(虫爪兰花指) -因为你美...
    9ea80b401870阅读 230评论 0 0
  • 01 美食街里美食摊,美食摊前美食女。 美食女呀卖美食,卖的美食换吃钱。 饿时只在摊前坐,饱时只在摊前眠。 半饿半...
    昵语浓浓浓阅读 722评论 0 1
  • “我的激情所在是打造一家可以传世的公司,这家公司里的人动力十足地创造伟大的产品。其他一切都是第二位的。当然,能赚钱...
    tjwang2008阅读 179评论 0 0