添加 虛線邊框|單邊框

在iOS中某些控件是具備直接使用 layer.boder 屬性設(shè)置邊框的寬度和顏色即可添加實(shí)現(xiàn)邊框;并且可以實(shí)現(xiàn)圓角邊框。

但是虛線邊框的實(shí)現(xiàn),只有依靠iOS具備的繪制功能實(shí)現(xiàn)。

具體實(shí)現(xiàn)代碼如下:

CGSize screenSize = [UIScreen mainScreen].bounds.size;
CGFloat viewWidth = 200;
CGFloat viewHeight = 200;
UIView *view = [[UIView alloc] initWithFrame:CGRectMake((screenSize.width - viewWidth)/2, (screenSize.height - viewHeight) / 2, viewWidth, viewHeight)];
view.backgroundColor = [UIColor colorWithWhite:0.9 alpha:1];
view.layer.cornerRadius = CGRectGetWidth(view.bounds)/2;

CAShapeLayer *borderLayer = [CAShapeLayer layer];
borderLayer.bounds = CGRectMake(0, 0, viewWidth, viewHeight);
borderLayer.position = CGPointMake(CGRectGetMidX(view.bounds), CGRectGetMidY(view.bounds));
//
//    borderLayer.path = [UIBezierPath bezierPathWithRect:borderLayer.bounds].CGPath;
borderLayer.path = [UIBezierPath bezierPathWithRoundedRect:borderLayer.bounds cornerRadius:CGRectGetWidth(borderLayer.bounds)/2].CGPath;
borderLayer.lineWidth = 1. / [[UIScreen mainScreen] scale];
//虛線邊框
borderLayer.lineDashPattern = @[@8, @8];
//實(shí)線邊框
//    borderLayer.lineDashPattern = nil;
borderLayer.fillColor = [UIColor clearColor].CGColor;
borderLayer.strokeColor = [UIColor redColor].CGColor;
[view.layer addSublayer:borderLayer];
//
[self.view addSubview:view];
  • 在給定某個(gè)UI控件添加邊框時(shí),是4條邊全部添加,但是如果只要添加某一條邊框如何實(shí)現(xiàn)?
可使用添加一個(gè)layer去添加到對(duì)應(yīng)邊框位置去:

CALyaer *borderLayer = [CALayer layer];
borderLayer.backgroundColor = [UIColor redColor];
[testUI.layer addSublayer:borderLayer];
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容