Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make UILabel in Swift a circle

Tags:

ios

swift

I am trying to make a UILabel in Swift a perfect circle. I am currently using the following:

pResult.layer.masksToBounds = true
pResult.layer.cornerRadius = 125

The problem with this is that it works fine on 6s plus but any other size it does not become a circle. What is the best way to do this?

like image 327
AppleTattooGuy Avatar asked Mar 16 '16 12:03

AppleTattooGuy


People also ask

How do I make labels rounded in Swift?

I assume you're setting layer. cornerRadius in the user defined runtime attributes and are expecting the background color rectangle to have rounded corners? Check if you've enabled "Clip to Bounds" in the view drawing options, that's off by default for labels but is required for the rounded corners to work.

What is UILabel in Swift?

A view that displays one or more lines of informational text.

How do you underline text in UILabel?

To make the text on UILabel underlined we will need to use the NSMutableAttributedString together with NSUnderlineStyleAttributeName. The Swift code snippet below demonstrates how to create a new UILabel programmatically and then use the NSMutableAttributedString to underline the text on the label.


2 Answers

If you you want to make perfect circle then first make sure your label width and height are same.

pResult.layer.cornerRadius = CGRectGetWidth(pResult.frame)/2
pResult.layer.masksToBounds = true

Reference

like image 76
Muzahid Avatar answered Oct 07 '22 13:10

Muzahid


If you are using swift 3 then please try this:

lblRoundDot.layer.cornerRadius = lblRoundDot.frame.width/2
lblRoundDot.layer.masksToBounds = true
like image 30
pansora abhay Avatar answered Oct 07 '22 11:10

pansora abhay