0

I have a variable text that has a background color and rounded corners. So I'm using UILabel with NSMutableAttributedString.

    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

Im getting a label with rounded corners only on one side. What should I do to get rounded corners on all sides ?

enter image description here

Sneha
  • 2,200
  • 6
  • 22
  • 36

1 Answers1

1

just increase the width of UILabel (myLabel)

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)
karthikeyan
  • 3,821
  • 3
  • 22
  • 45