iOS 使用CAShapeLayer给View添加虚线边框

Code: 

- (void)initUI{
    
    UILabel *tipLabel = [[UILabel alloc]init];
    tipLabel.text = @"您有什么问题或者建议想对我们说 ?";
    tipLabel.textColor = RGB(36, 137, 101);
    tipLabel.frame = lxy(20, 10, SCREEN_WIDTH-20*2, 20);
    tipLabel.font = [UIFont systemFontOfSize:13];
    [self.view addSubview:tipLabel];
    
    
    /**
     * 给客服建议输入框
     */
    _suggestView = [[UITextView alloc] initWithFrame:CGRectMake(20,tipLabel.bottom+10, SCREEN_WIDTH-2*20, 150)];
    _suggestView.returnKeyType = UIReturnKeyDone;
    _suggestView.layer.borderWidth = 1.0;
    _suggestView.layer.borderColor = RGB(36, 137, 101).CGColor;
    _suggestView.font = [UIFont systemFontOfSize:14];
    _suggestView.textColor =  [UIColor colorWithRed:134/255.0 green:135/255.0 blue:140/255.0 alpha:1.0];
    [_suggestView.layer setCornerRadius:5];
    [self.view addSubview:_suggestView];

    
    UIButton *submitBtn = [[UIButton alloc]init];
    submitBtn.backgroundColor = RGB(253, 204, 1);
    [submitBtn setTitle:@"提  交" forState:UIControlStateNormal];
    [submitBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    submitBtn.titleLabel.font = [UIFont systemFontOfSize:14];
    submitBtn.frame = lxy(0, _suggestView.bottom+20, SCREEN_WIDTH, 40);
    [submitBtn addTarget:self action:@selector(submitClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:submitBtn];
    
    
    
    //客服电话视图
    UIView *customerView = [[UIView alloc]init];
    customerView.backgroundColor = [UIColor clearColor];
    customerView.frame = lxy(20, submitBtn.bottom+50, SCREEN_WIDTH-20*2, 60);
    customerView.layer.cornerRadius = 5.f;
    [self.view addSubview:customerView];
    [self addBorderToLayer:customerView];
    
    
    UILabel *phoneLabel = [[UILabel alloc]init];
    phoneLabel.text = @"客户电话: xxxxxxx";
    phoneLabel.font = smallFont;
    phoneLabel.textColor = [UIColor darkGrayColor];
    CGSize phoneSize = [phoneLabel.text boundingRectWithSize:CGSizeMake(SCREEN_WIDTH,1000) options:NSStringDrawingUsesLineFragmentOrigin attributes:[NSDictionary dictionaryWithObject:phoneLabel.font forKey:NSFontAttributeName] context:nil].size;
    phoneLabel.frame = lxy(customerView.width/2 -phoneSize.width/2-15, 20, phoneSize.width, 20);
    [customerView addSubview:phoneLabel];
    
    UIImageView *phoneImageV = [[UIImageView alloc]init];
    phoneImageV.image = [UIImage imageNamed:@"login_phone"];
    phoneImageV.frame = lxy(phoneLabel.right+10, 15, 20, 30);
    [customerView addSubview:phoneImageV];
    
    UITapGestureRecognizer *callTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(telPhone)];
    customerView.userInteractionEnabled = YES;
    [customerView addGestureRecognizer:callTap];
}

//打电话
- (void)telPhone{
    
    UIWebView* callWebview =[[UIWebView alloc] init];
    NSURL *telURL =[NSURL URLWithString:@"tel:xxxxxxxx"];
    [callWebview loadRequest:[NSURLRequest requestWithURL:telURL]];
    [self.view addSubview:callWebview];
    
}


- (void)addBorderToLayer:(UIView *)view{
    
    CAShapeLayer *border = [CAShapeLayer layer];
    //  线条颜色
    border.strokeColor = RGB(36, 137, 101).CGColor;
    border.fillColor = nil;
    border.path = [UIBezierPath bezierPathWithRect:view.bounds].CGPath;
    border.frame = view.bounds;
    border.lineWidth = .6f;
    border.lineCap = @"square";
    //  第一个是 线条长度   第二个是间距    nil时为实线
    border.lineDashPattern = @[@9, @4];
    [view.layer addSublayer:border];
}


效果如如下



参考地址:http://www.jianshu.com/p/b8c92aeab185




猜你喜欢

转载自blog.csdn.net/lixianyue1991/article/details/76585312