2017-09-07 8 views
-1

UILabel을 프로그래밍 방식으로 생성 중입니다. 그러나 아래의 코드는 내가 둥근 모서리를주지는 않습니다. 나는 아주 기본적인 것을 놓치고 있다고 생각합니다.UILabel swift의 둥근 모서리

var textLabel:UILabel? = UILabel() 
textLabel?.text = text 
textLabel?.frame = CGRect(x:point.x, y:point.y, width:(textLabel?.intrinsicContentSize.width)!, height:15) 
textLabel?.backgroundColor = UIColor.white 
textLabel?.font = UIFont(name:"OpenSans", size:8) 
textLabel?.sizeToFit() 
textLabel?.layer.cornerRadius = 20.0 

누구나 올바른 방향으로 나를 가리킬 수 있습니까?

+5

가능한 복제 ([I는 둥근 모서리 CGRect (스위프트)와 함께있는 UIImage/-보기를 어떻게해야합니까] https://stackoverflow.com/questions/25476139/how-do-i-make -a-uiimage-view-rounded-corners-cgrect-swift) –

답변

6

난 당신이 textLabel라는위한 maskToBounds를 설정한다고 생각합니다. 이 시도 :

textLabel?.layer.masksToBounds = true 
1

masksToBounds이 서브 레이어는 레이어의 경계에 클리핑 여부를 나타내는 부울 역할을 라벨에 대한 masksToBounds을 설정합니다.

textLabel?.layer.cornerRadius = 20.0 
textLabel?.layer.masksToBounds = true 

사과 documents를 참조하십시오.

yourLabel.layer.cornerRadius = 8.0 
yourLabel.layer.masksToBounds = true 
yourLabel.layer.borderColor = UIColor.white.cgColor 
yourLabel.layer.borderWidth = 1.0 

이 당신에게 둥근 테두리

의 핵심은 서브 레이어는 레이어의 경계에 클리핑되어 있는지 여부를 나타내는 부울는 해당 부동산 "maskToBounds"입니다을 제공한다 :

2

이 시도 : -

textLabel?.layer.cornerRadius = textLabel?.frame.size.height/2.0 

textLabel?.layer.masksToBounds = true 

당신이 다음 테두리 색상을 설정하려면

: -

textLabel?.layer.borderColor = .red.cgColor 
    textLabel?.layer.borderWidth = 1.0 
+0

질문자는 이미 'layer.cornerRadius = 20.0' 크기를 언급했습니다. –

+0

정확한 코너를 원할 때 사용합니다. 크기가 높이보다 크면 완벽한 모양으로 크기가 조절되지 않습니다. 또는 필요에 따라 상수 값을 사용할 수 있습니다. – Pushpendra