2016-07-25 4 views
0

uilabel에 테두리를 추가하려고했지만 위쪽, 오른쪽 및 아래쪽 테두리 만 갖고 싶습니다. 이처럼UILabel 아래쪽 및 오른쪽 테두리

는 :

     | 
     I am a label | 
         | 
     ---------------- 

나는이 코드를 사용하려고하지만, UILabel의의 서브 클래스를 만들고 다음 코드를 추가하여 기본

myLabel.layer.borderWidth = 1; 
myLabel.layer.borderColor = UIColorCode.init(hexString: "#666666") 
+0

그것은하지 "기본적으로"코드가 무엇을 먹으 렴 그것은 결코 변하지 않습니다. 전체 요소 주위에 테두리를 추가합니다. 당신이 원하는 것을 성취하는 것은 온라인에서 잘 문서화되어 있으며 stackoverflow에 대한 많은 질문이 있습니다. 질문을하기 전에 연구를하거나 인터넷 검색을 해본 적이 있습니까? –

답변

0

모든 4 개면을 추가합니다. 필요한 경우 경계선이 그려집니다.

override func drawRect(rect: CGRect) { 

     let outerBorder = UIColor.blackColor() 
     let lineWidth : CGFloat = 2.0 
     let insetRect = rect.insetBy(dx: lineWidth/2, dy: lineWidth/2) 
     let startingTopPoint = CGPointMake(insetRect.origin.x,insetRect.origin.y) 
     let endingTopPoint = CGPoint(x: insetRect.maxX, y: insetRect.minY) 

     let bottomLeft = CGPoint(x: insetRect.minX, y: insetRect.maxY) 
     let bottomRight  = CGPoint(x: insetRect.maxX, y: insetRect.maxY) 


     let path = UIBezierPath() 
     path.moveToPoint(startingTopPoint) 
     path.addLineToPoint(endingTopPoint) 
     path.lineWidth = 2.0 
     path.addLineToPoint(bottomRight) 
     path.addLineToPoint(bottomLeft) 


     outerBorder.setStroke() 
     path.stroke() 
} 
0
let borderWidth: CGFloat = 1.0 

let borderLayer = CAShapeLayer() 
borderLayer.lineWidth = borderWidth 
borderLayer.fillColor = UIColor.clearColor().CGColor 
borderLayer.strokeColor = UIColor.blueColor().CGColor 

let borderLine = UIBezierPath() 
borderLine.moveToPoint(CGPoint(x: 0, y: myLabel.bounds.height - borderWidth/2)) 
borderLine.addLineToPoint(CGPoint(x: myLabel.bounds.width - borderWidth/2, y: myLabel.bounds.height - borderWidth/2)) 
borderLine.addLineToPoint(CGPoint(x: myLabel.bounds.width - borderWidth/2, y: 0)) 

borderLayer.path = borderLine.CGPath 

myLabel.layer.addSublayer(borderLayer) 
관련 문제