2017-03-23 2 views
-1

배경색과 둥근 모서리가있는 변수 텍스트가 있습니다. 그래서 NSMutableAttributedString 함께 UILabel 사용하고 있습니다.NSMutableAttributedString에서 UILabel로 모서리를 둥글게했습니다.

myAttributedString = NSMutableAttributedString(string:"hello") 
    myAttributedString.addAttribute(NSBackgroundColorAttributeName, value: UIColor.red, range: NSRange(location: 0, length: myAttributedString.length)) 
    myLabel.attributedText = myAttributedString 
    myLabel.layer.cornerRadius = 5 
    myLabel.clipsToBounds = true 
    myLabel.layer.masksToBounds = true 

한쪽에만 모서리가 둥근 레이블이 있습니다. 모든면에서 모서리를 둥글게하려면 어떻게해야합니까?

enter image description here

+1

이 시도 UILabel의 (myLabel)의 폭을 증가 : http://stackoverflow.com/questions/37768686/convert-a-string-to-nsattributedstring -in-a-specific-manner – karthikeyan

+0

@ karthikeyan 나는 cornerRadius와 masktoBounds를 추가했습니다. sizeToFit을 추가하려고했는데 ... – Sneha

+0

문제는 다른 곳에서 당신의 라벨에 uibeizerpath를 사용하고 있습니까 –

답변

0

단지

let myAttributedString = NSMutableAttributedString(string:"hello") 
     myAttributedString.addAttribute(NSBackgroundColorAttributeName, value: UIColor.red, range: NSRange(location: 0, length: myAttributedString.length)) 
     myLabel.attributedText = myAttributedString 
     myLabel.layer.cornerRadius = 5 
     myLabel.clipsToBounds = true 
     myLabel.layer.masksToBounds = true 

// your code is right 
// just increase the width of UILabel (myLabel) 
+0

언제든지 내용이 변경되므로 고정 된 너비를 내 라벨에 설정할 수 있습니다. – Sneha

관련 문제