How to use rounded corner label in iphone, UILabel Round Corners
Asked Answered
B

5

12

I am using some labels in a view. I want to make rounded corner label in my iphone application. I use following code to do this but it's not work. I got have some errors to use that properties.

label.layer.borderColor = [UIColor blueColor].CGColor;
label.layer.borderWidth = 4.0;
label.layer.cornerRadius = 8;
Bigham answered 16/12, 2011 at 5:19 Comment(2)
How about putting an rounded corner image as background of the uilabel??Heliacal
Possible duplicate of How do I create a round cornered UILabel on the iPhone?Emelun
B
11

Hard to know for sure what you're asking as you didn't include the errors you're getting. Have you added the QuartzCore framework to your project and #import <QuartzCore/CALayer.h> to the file modifying the layer? If that's not it, add the errors and more info to your question.

EDIT: you can also #import <QuartzCore/QuartzCore.h> as suggested in the comments. QuartzCore.h includes CALayer.h along with the rest of the QuartzCore components.

Beavers answered 16/12, 2011 at 5:25 Comment(2)
+1) importing #import <QuartzCore/QuartzCore.h> will remove all these type of errors.Heredes
@mohit You should accept XJones answer. Nobody will solve your problem if you not are ready to give them the credit for it.Gastrulation
C
20

I was facing the same problem, using a UILabel with backgroundColor in a cell, and added this to work correctly :

label.layer.cornerRadius=8.0;
label.clipsToBounds=YES;
Clie answered 5/1, 2015 at 15:26 Comment(1)
Should be cell.offLimitsLabel.clipsToBounds=trueCondonation
C
13

Just add #import <QuartzCore/QuartzCore.h> in your .m file

and suppose you have a UILabel *myLabel;

just do [myLabel.layer setCornerRadius:20]; //value '20' can be changed according to your wish :)

Crossquestion answered 16/12, 2011 at 5:37 Comment(2)
you can vote up or mark as correct answer. it will be good for other people who have same problem like yours, and can refer your SO post.Thank youCrossquestion
Just one tip (I had to add this line to make it work): myLabel.layer.masksToBounds = YES;Rottweiler
B
11

Hard to know for sure what you're asking as you didn't include the errors you're getting. Have you added the QuartzCore framework to your project and #import <QuartzCore/CALayer.h> to the file modifying the layer? If that's not it, add the errors and more info to your question.

EDIT: you can also #import <QuartzCore/QuartzCore.h> as suggested in the comments. QuartzCore.h includes CALayer.h along with the rest of the QuartzCore components.

Beavers answered 16/12, 2011 at 5:25 Comment(2)
+1) importing #import <QuartzCore/QuartzCore.h> will remove all these type of errors.Heredes
@mohit You should accept XJones answer. Nobody will solve your problem if you not are ready to give them the credit for it.Gastrulation
A
5

This simple code enough for RoundLabel

LabelName.layer.cornerRadius = LableName.frame.size.height/2;
LabelName.layer.masksToBounds = YES;
Allyn answered 19/5, 2015 at 6:58 Comment(0)
A
1

I would create a view with rounded corners and add the label to that view.

Adrenalin answered 16/12, 2011 at 5:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.