Is there a built in way to create round-cornered UILabels? If the answer is no, how would one go about creating such an object?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
iOS 3.0 and later
iPhone OS 3.0 and later supports the
cornerRadiusproperty on theCALayerclass. Every view has aCALayerinstance that you can manipulate. This means you can get rounded corners in one line:You will need to
#import <QuartzCore/QuartzCore.h>and link to the QuartzCore framework to get access to CALayer’s headers and properties.Before iOS 3.0
One way to do it, which I used recently, is to create a UIView subclass which simply draws a rounded rectangle, and then make the UILabel or, in my case, UITextView, a subview inside of it. Specifically:
UIViewsubclass and name it something likeRoundRectView.RoundRectView‘sdrawRect:method, draw a path around the bounds of the view using Core Graphics calls like CGContextAddLineToPoint() for the edges and and CGContextAddArcToPoint() for the rounded corners.UILabelinstance and make it a subview of the RoundRectView.label.frame = CGRectInset(roundRectView.bounds, 8, 8);)You can place the RoundRectView on a view using Interface Builder if you create a generic UIView and then change its class using the inspector. You won’t see the rectangle until you compile and run your app, but at least you’ll be able to place the subview and connect it to outlets or actions if needed.